Do you know if the bad XML is always of the same structure so that its removal can be automated? Part of your problem is that XML parsers are supposed to die horribly if they encounter badly formed XML. There are XML-ish things out there that have their own parsers as a result of this.

XML::Twig violates the "die on bad xml" rule by offering calls that at least return from a failure and give you the error message rather than dieing, so that you might be able to recover from the failure with an automated fix: e.g.

if (!safeparse($my_stuff)){handle_errors();}

where handle_errors() checks the message and then runs some sort or preprocessor to remove the offending lines, then calls safeparse() again. It's a bit of a pain because it means you have to re-run all the stuff that you successfully parsed, but it's better than nothing.

You might also experiment with using one of the HTML parsers to extract what you want. They're not likely to be as good with enormous files they should be more tolerant of bad behavior.

And if you have a way to contact whoever is generating the files, you might point out that some of them are badly formed and that they might have a bug in their xml generator. If anyone else is using the files, they're probably running into similar problems.


In reply to Re: Ignoring not well-formed (invalid token) errors by bitingduck
in thread Ignoring not well-formed (invalid token) errors by brettski

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.