A fast Perl way to test if XML is well-formed (as opposed to validated) is to use Cooper and Wall's venerable XML::Parser::Expat.

Its documentation states:

setHandlers(TYPE, HANDLER [, TYPE, HANDLER ...])

This method registers handlers for the various events. If no handlers are registered, then a call to parsestring or parsefile will only determine if the corresponding XML document is well formed (by returning without error.) This may be called from within a handler, after the parse has started.
Reviews of expat often state that one of its advantages is that it is fast. (For the record, expat is in C rather than in Perl.)

It is hard to imagine Larry Wall, who wrote version 1.0 of XML::Parser::Expat in order to provide "lowlevel access to James Clark's expat XML parser," bloating his code. Borrowing Davorg's suggested solution, the test would then be:

$parser = new XML::Parser::Expat; $parser->setHandlers(); open(FOO, 'info.xml') or die "Couldn't open"; eval { $parser->parse(*FOO) }; if ($@) { print "$_ is bad\n"; } else { print "$_ is good\n"; } close(FOO);

In reply to Re: well formed xml by sierrathedog04
in thread well formed xml by marvell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.