Programmer's Cookbook

Recipes for the practical programmer

Monday, February 13, 2006

 

Test Well-Formedness of XML with Perl

Sometimes you run into a problem where a tool that you are using reports an error with an XML file, but doesn't report the exact location. When this happens you need to use some other tool to text the structure of the XML file, and report the exact location of the error. When you don't have a tool available for this task, you can use Perl at the command line.

This will read the file config.xml, and report any errors that it finds. If no errors are found, it will return nothing.

perl -MXML::Parser -e '$x = XML::Parser->new;$x->parsefile($ARGV[0])' config.xml



Sample output of the command, reporting the error at the 6th character of line 36:

mismatched tag at line 32, column 6, byte 1078
at /usr/lib/perl5/vendor_perl/5.8/cygwin/XML/Parser.pm line 187



This command takes it a little further. Not only will it show the location of the error, but it will also print out debugging information for the parser, which may be useful on some cases.

perl -MXML::Parser -e '$x = XML::Parser->new(Style=>"Debug");$x->parsefile($ARGV[0])' config.xml


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?