Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How can i validate xml file?

by Anonymous Monk
on Feb 23, 2006 at 15:46 UTC ( #532297=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am new to perl. I am using perl to validate my xml file. I heard about XML::LibXML and XML::Simple module by using the above both module or using some other module, is it possible to validate the xml file..Whether it's correct xml file or parsing error?

I want to validate only xml file. without DTD or XSD file.

Please give me the sample code or link..

Thanks in Advance

Replies are listed 'Best First'.
Re: How can i validate xml file?
by davorg (Chancellor) on Feb 23, 2006 at 16:00 UTC

    All XML processors will throw an exception (i.e. die) if they come across malformed XML (they have to, it's in the spec). So the way to check for valid XML is to try to parse the file and catch any exceptions thrown (using eval). This works with any of the Perl XML processing modules.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How can i validate xml file?
by monkey_boy (Priest) on Feb 23, 2006 at 16:03 UTC
    Not perl, but if your on a linux os, then check if xmllint is installed, this can be used to validate your xml in superfast time,

    system("xmllint --noout foo.xml") and die "Failed!";


    This is not a Signature...

      Thanks monkey_boy, and davido now i get answer from davidrw answer chanceless thanks davidrw.

      use XML::LibXML; my $parser = XML::LibXML->new(); my $xml_text = eval { $parser->parse_string($text); }; print $@

      Hi monkey_boy,

      Thanks kool, It's working fine. But, I am getting xml content is it possible to check. And also, i am not allow here to use system function i want sample code.. For ex if i validate xml file with xsd file i use below code like i want code please

      use XML::LibXML; my $schema_file = 'po.xsd'; my $document = 'po.xml'; my $schema = XML::LibXML::Schema->new(location => $schema_file); my $parser = XML::LibXML->new; my $doc = $parser->parse_file($document); eval { $schema->validate($doc) }; die $@ if $@; print "$document validated successfully\n";

        If your input data is not well-formed XML (and I assume that's what you're asking about) then the parse_file call will die. Put a eval around that call and check the value in $@ after parse_file returns.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: How can i validate xml file?
by grantm (Parson) on Feb 23, 2006 at 23:38 UTC

    You seem to be wanting to check that an XML document is 'well formed'. Here's a Perl one-liner that will do that:

    perl -MXML::Parser -e "XML::Parser->new( ErrorContext => 3 )->parsefil +e(shift)" your_file.xml

    If you were incorporating it into a larger script, then you'd want to wrap it in eval { ... } and check the contents of $@ afterwards.

    One trap with using XML::Simple for well-formedness checks is that the SAX parser included in the latest release of XML::LibXML does not die on all parse errors.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-12-14 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (69 votes). Check out past polls.

    Notices?