perl xml check for element closure

perl xml check for element closure

am 19.11.2006 03:26:03 von inetquestion

I'm interested in writing a perl script that checks an xml document for
proper element closure only. There is no dtd associated, so this is
really the only type validation I could think of... Is there an easy
way to do this via perl? All I'm looking for is a simple 0 or 1 return
code.

Regards,

-Inet

Re: perl xml check for element closure

am 19.11.2006 03:41:47 von inetquestion

I just found the code below which appears to be exactly what I was
looking for... As soon as I get back to my home pc I'll give it a
shot.

-Inet

----------------






use XML::Parser;

my $xmlfile = shift @ARGV; # the file to parse

# initialize parser object and parse the string
my $parser = XML::Parser->new( ErrorContext => 2 );
eval { $parser->parsefile( $xmlfile ); };

# report any error that stopped parsing, or announce success
if( $@ ) {
$@ =~ s/at \/.*?$//s; # remove module line number
print STDERR "\nERROR in '$xmlfile':\n$@\n";
} else {
print STDERR "'$xmlfile' is well-formed\n";
}