Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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>http://builder.com.com/article.jhtml?id=..htm</url> <title>Remedial XML for programmers: Basic syntax</title> <summary>In this first installment in...</summary> </article> </series>
OUTPUT is:
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!

Comment on XML validating (no DTD)
Select or Download Code
Re: XML validating (no DTD)
by Plankton (Priest) on Sep 02, 2004 at 15:48 UTC
Re: XML validating (no DTD)
by ikegami (Pope) on Sep 02, 2004 at 15:57 UTC

    Change

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

    to

    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 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.

      Hi

      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://387941]
Approved by ChemBoy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2015-05-28 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    In my home, the TV remote control is ...









    Results (562 votes), past polls