Archive

Posts Tagged ‘perl xml’

Perl XML well formed check

February 3rd, 2010 dwright No comments

Problem space:

Using Perl, you want to see if XML is 'valid'.

(it's actually just a 'well formed' check. To check validity, you would need to use the XML documents DTD, if one exists)

my $parser = XML::Parser->new( ErrorContext => 2 );
eval { $parser->parse( $data ); };
# report any error that stopped parsing
if( $@ ) {
    Carp::croak "ERROR: NOT WELL FORMED ELEMENT: $@\n";
}

Additionally, if you have a *NIX box available, xmllint should be installed, is available from the command line.

Categories: perl Tags: