my $parser = XML::LibXML->new; $parser->validation(1); eval { $doc = $parser->parse_string($xml); }; if ($@) { # XML not well formed, error handling here... } eval { $doc->validate; }; if ($@) { # XML did not pass DTD validation, error handling here... }