perl xml check for element closure

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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



Re: perl xml check for element closure

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



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";

Site Timeline