3

I know there are quite some threads talking about validating XML file against its XML schema, such as : validate-xml-using-libxml and xml-schema-validation-with-relaxng

So if there is a simple Perl module on CPAN that can test this with minimal code, then that would be very fantastic to know.

Community
  • 1
  • 1
Michael Mao
  • 9,878
  • 23
  • 75
  • 91
  • possible duplicate of [Is there a Perl module that validates an XML against a schema?](http://stackoverflow.com/questions/322080/is-there-a-perl-module-that-validates-an-xml-against-a-schema) – Ether Oct 19 '10 at 16:22

4 Answers4

7

You can simply use XML::Parser.

Note that your question title refers to being well-formatted (well-formed?) whereas the body refers to validation. If you want to validate (against a schema) check out XML::Validator::Schema.

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Thanks for this quick answer and sorry about the wrong term, yeah, you are right, I mean well-formed not well-formatted :) – Michael Mao Oct 18 '10 at 23:25
2

I'd use Test::XML. It gives you methods is_xml, is_well_formed_xml.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
jira
  • 3,890
  • 3
  • 22
  • 32
  • Test::XML is for writing test scripts ala Test::More and Test::Builder, and as such isn't very useful outside of a test script. But you can examine its source code to see how to use XML::Parser to check if your XML is well-formed. – cjm Oct 19 '10 at 09:01
1

There's XML::Compile, but it's not easy until you learn to use the module.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
0

Since XML::Parser will die if a parsing error occurs, try this for minimal code:

XML::Parser->new->parse($xml);