Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

XML validating (no DTD)

by 2ge (Scribe)
on Sep 02, 2004 at 15:28 UTC ( #387941=perlquestion: print w/replies, xml ) Need Help??
2ge has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

I know how to validate XML, but still error massages appears on STDOUT, anyone knows how to avoid them ? my code looks:
use strict; use XML::Checker::Parser; my $xml_file = 'data.xml'; my $xp = new XML::Checker::Parser ( Handlers => { } ); eval { $xp->parsefile($xml_file); local $XML::Checker::FAIL = \&my_fail; }; if ($@) { print "$xml_file failed validation!\n"; } else { print "$xml_file passed validation\n"; } sub my_fail { my $code = shift; die XML::Checker::error_message ($code, @_) if $code < 300; }
data.xml is:
<?xml version="1.0" encoding="utf-8"?> <series> <article> <url></url> <title>Remedial XML for programmers: Basic syntax</title> <summary>In this first installment in...</summary> </article> </series>
XML::Checker ERROR-101: undefined ELEMENT [series] Context: line 2, column 0, byte 40 XML::Checker ERROR-101: undefined ELEMENT [article] Context: line 3, column 6, byte 56 XML::Checker ERROR-101: undefined ELEMENT [url] Context: line 4, column 13, byte 80 XML::Checker ERROR-101: undefined ELEMENT [title] Context: line 5, column 13, byte 167 XML::Checker ERROR-101: undefined ELEMENT [summary] Context: line 6, column 13, byte 239 data.xml passed validation
I dont't want to see error messages.
thanks a lot!

Replies are listed 'Best First'.
Re: XML validating (no DTD)
by ikegami (Pope) on Sep 02, 2004 at 15:57 UTC


    $xp->parsefile($xml_file); local $XML::Checker::FAIL = \&my_fail;


    local $XML::Checker::FAIL = \&my_fail; $xp->parsefile($xml_file);

    It does no good to set the error handler after the checking is done.

Re: XML validating (no DTD)
by Plankton (Vicar) on Sep 02, 2004 at 15:48 UTC
Re: XML validating (no DTD)
by iburrell (Chaplain) on Sep 02, 2004 at 16:47 UTC
    I don't see any DTD declared in the XML or the code. If you don't have a schema to validate against, you can't validate an XML document. Checking for well-formed is different, and something most parsers should report on. XML::Checker::Parser looks like it only validates against DTDs declared in the document.

    You can also validate against XML Schema or RELAX NG. I don't know of any pure Perl code for those, but calling external programs and libraries work well.


      I just found the solution on perlmonks, it is so easy:
      use XML::Parser; my $p = XML::Parser->new; my $file = 'data.xml'; eval { $p->parsefile($file) }; if ($@) { print 'XML is not well formed'; } else { print 'XML is well formed'; }

      Ofcoure, I can't validate XML, if I don't have DTD, I can check it if it is well formed, sorry for mistake.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://387941]
Approved by ChemBoy
[erix]: ok, but mainainability comes after it has been made to work :)
[erix]: maybe I should have cobbled together a more simple example
[erix]: the main question is whether repeating groups get captured. I thought they did but it looks they do not.
[erix]: or they probably get overwritten when the repeating is done

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-01-19 08:39 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (216 votes). Check out past polls.