Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Validating XML

by networker2149 (Novice)
on Dec 03, 2011 at 21:57 UTC ( #941574=perlquestion: print w/replies, xml ) Need Help??

networker2149 has asked for the wisdom of the Perl Monks concerning the following question:

I got an error recently after modifying a script I wrote to parse XML from juniper router commands. The change was to run a different show command that produces more information. The problem is that routers are apparently bad at XML and this caused the mal-formatted XML I'm used to to turn into invalid XML. Will post more code if required but It worked pretty well before the invalid characters. I put in a regex to remove the invalid character, but I was curious if there was a way to do this automatically. Or at least another module that provides better error handling. Here's the error: reference to invalid character number at line 1, column 1373, byte 1373 at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/XML/Parser.pm line 187 Any advice would be helpful. Some clue as to how to zero in on line 1, column 1373, byte 1373 of a file would be helpful as well. Thanks

use XML::Simple; my $xml = new XML::Simple (KeyAttr=>[]); my $data = $xml->XMLin("cmd-out\/$hostname.xml");

Replies are listed 'Best First'.
Re: Validating XML
by ikegami (Pope) on Dec 03, 2011 at 22:05 UTC
    XML::LibXML has an option to recover from errors you might find useful.
Re: Validating XML
by JavaFan (Canon) on Dec 03, 2011 at 23:05 UTC
    Some clue as to how to zero in on line 1, column 1373, byte 1373 of a file would be helpful as well.
    Untested code:
    use autodie; use Fcntl ':seek'; open my $fh, "<:raw", "your-file-name-goes-here"; seek $fh, 1373, SEEK_SET; read $fh, $_, 1; # No sysread! print $_, "\n";
Re: Validating XML
by vinian (Beadle) on Dec 04, 2011 at 06:47 UTC
    use XML::Simple; my $xml = new XML::Simple (KeyAttr=>[]); my $data; eval { $data = $xml->XMLin("cmd-out\/$hostname.xml"); }; if ( $@ ){ die "$@"; }

    use eval catch error

Re: Validating XML
by grantm (Parson) on Dec 04, 2011 at 23:25 UTC

    I use the following Perl 1-liner to help identify the offending part of a broken XML file:

    perl -MXML::Parser -e "XML::Parser->new( ErrorContext => 3 )->parsefil +e(shift)" filename.xml

    The diagnostic output from XML::Parser includes an ASCII arrow pointing to the bad bit. Of course if the line in question is over 1300 bytes long then that might be a little hard to read :-(

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://941574]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-01-20 21:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?