Michael Mao
Michael Mao

Reputation: 10168

How can I easily test if XML file is well-formed in Perl?

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.

Upvotes: 3

Views: 6331

Answers (4)

Brian Agnew
Brian Agnew

Reputation: 272337

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.

Upvotes: 7

Deven T. Corzine
Deven T. Corzine

Reputation: 607

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

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

Upvotes: 0

jira
jira

Reputation: 3944

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

Upvotes: 2

brian d foy
brian d foy

Reputation: 132858

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

Upvotes: 1

Related Questions