former33t has asked for the wisdom of the Perl Monks concerning the following question:
I have to parse XML formatted messages being sent across a network connection and prefer to use XPath. I thought I was being clever by using code like the below to avoid a fatal error in the network server while parsing the messages:
use XML::XPath; my ($xml, $xp); $xml = “not xml”; eval { $xp = XML::XPath->new(xml => $xml); }; if ($@) { print “Parsing failed\n”; } …..
The problem is that apparently XML::XPath doesn’t check for valid XML on object creation. It returns the object fine, which is far from ideal for my needs. I don’t think anyone will ever send me invalid XML since I wrote the client apps too, but I don’t want to bet a midnight call in on it either. The solution involves also using XML::Parser and letting it do the parsing. If everything is okay, then we create an XPath object.
use XML::XPath; use XML::Parser; my ($xml, $xp); $xml = “not xml”; eval { my $po = new XML::Parser; $po->parse($xml); }; if ($@) { print “Parsing failed\n”; #handle error } else { $xp = XML::XPath->new(xml => $xml); } …..
The XML::XPath documentation says that the constructor will take a parser argument that is an XML::Parser object but this seems to work just fine too (although probably has higher overhead). What would be ideal is if XML::XPath just did XML validation before returning the object.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::XPath doesn't validate XML???
by jettero (Monsignor) on May 19, 2007 at 20:09 UTC | |
|
Re: XML::XPath doesn't validate XML???
by Cody Pendant (Prior) on May 20, 2007 at 03:33 UTC | |
by former33t (Scribe) on May 21, 2007 at 13:10 UTC |