Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Which XML Validation to use, for Perl

by brycen (Monk)
on Nov 03, 2008 at 21:07 UTC ( [id://721194]=perlquestion: print w/replies, xml ) Need Help??

brycen has asked for the wisdom of the Perl Monks concerning the following question:

Oh Monks,

I'm seeking the wisdom of the ages... which of the many paths is the best for me?

I simply need a validation step after generating an XML string in perl. The code currently pipes the string out to "java dom.DOMCount -v" to do this.

Should I compile my own http://xerces.apache.org/xerces-p/ Or the (seemingly old and totally undocumented) http://search.cpan.org/dist/XML-Xerces/? Or shell out to the C version of Xerces? Or libxml-checker-perl? Or?

I'd prefer something I can install from a Debian package on Etch. Is there anything that's prepackaged? It seems the C version of Xerces requires hand compiling utilities like DOMCount. And XML-Xerces is like 20 lines of code just to validate -- and you ahve to read the C documentation, which does not quite line up with the perl reality.

Or put another way: given a $xml string in perl, how can I validate it as XML?

Replies are listed 'Best First'.
Re: Which XML Validation to use, for Perl
by GrandFather (Saint) on Nov 03, 2008 at 21:50 UTC

    Why do you need to validate Perl generated XML? Are you not using one of the XML writing modules?

    There are a large number of Perl modules available for working with XML. Of immediate interest may be XML::Twig, XML::Parser or XML::LibXML for validating XML documents. For writing XML there are a plethora of modules in various stages of creation. XML::Writer is probably a pretty good starting point.


    Perl reduces RSI - it saves typing
      It's hand rolled legacy XML, created by string concatenation. Thus, the need for a final "is it valid" check before shipping it off. I'm interested at this point only in validation -- not parsing, not XML generation.

      The plethora of modules is a problem. What's the easy, simple, robust and mature recommendation on the best one to use?

Re: Which XML Validation to use, for Perl
by salva (Canon) on Nov 03, 2008 at 22:00 UTC
    Try XML::LibXML that's a wrapper around Gnome libxml2. It's available for Debian as libxml-libxml-perl.

    It can validate XML against RelaxNG schemas also.

Re: Which XML Validation to use, for Perl
by Your Mother (Archbishop) on Nov 03, 2008 at 22:04 UTC
Re: Which XML Validation to use, for Perl
by dHarry (Abbot) on Nov 04, 2008 at 08:52 UTC

    XML validation in Perl is a bit limited compared to other programming languages (IMHO). Besides the LibXML alternative suggested you might want to investigate XML::Validate. This looks like a promising initiative. I like the idea of a generic interface to different XML validation backends dependding on the platform you work on.

      XML validation in Perl is a bit limited compared to other programming languages (IMHO)
      Please elaborate, because the way I see it there is always
      system qw[other-language-validate options file.xml ];
Re: Which XML Validation to use, for Perl
by brycen (Monk) on Nov 04, 2008 at 18:03 UTC
    What do people think of XML::Checker::Parser?
    #!/usr/bin/perl -w use strict; use XML::Parser; use XML::Checker::Parser; my $xml = qq(<?xml version='1.0' encoding='iso-8859-1' ?> <!DOCTYPE DISS_order SYSTEM 'umi_orders.dtd'> <DISS_order> </DISS_order> ); my ($p1, $result); $p1 = new XML::Checker::Parser(); eval { $p1->parse($xml); }; if ($@) { print "Error\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-18 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found