I receive XML files from my client with a list of orders. The XML (over which I have no control) may or may not contain some dodgy characters, which would cause XML parsing to fail.
However, I would like to process all the orders that I can, and report errors on the dodgy ones.
My understanding is that, if I use
then the file will be parsed quickly, but either succeed or fail in its entirety.$parser = XML::LibXML->new(); $doc = $parser->parse_file( $xmlfilename );
I was thinking about using this as the first method, for speed, and if it fails, resort to something like:
Or should I be creating one master document and importing/adopting nodes? Or a different approach entirely? thanks$p = XML:LibXML->new(); local $/='</order>'; open (FH,'<:utf8',$filename) or die $!; while (my $order = <FH>) { $order=~s/^.*?<order>/<order>/gs; my $xml = <<XML; <?xml version="1.0" encoding="UTF-8"?> <orders> $order </orders> XML my $doc = eval {$parser->parse_string($xml)}; if ($@) { warn ("error : $@"); next; } process_orders($doc); }
In reply to Parsing dodgy XML by clinton
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |