Venerable Denizens of the Monastary --

I am working on a project to take scanned articles from an academic journal which have been marked up in XML by hand and do nifty things with the documents (i.e., produce HTML, TeX, and PDF output; pull abstracts from the articles; cross reference the articles and so on). The articles have APA style references such as (author1 & author2, 1999) embedded in the text and the occasional article has a bare > or < as part of the text. I am using mirod's excellent XML::Twig module to parse and process the data. As you would expect, the parser dies when it comes across an &, >, or <. For example, if I have the following code with an XML sample embedded as a here doc --

use strict; use warnings; use XML::Twig; my $xml = <<XML; <reference> <authors> <author>Lindsley, O.R.</author> </authors> <year>1992</year> <title>Precision teaching: Discoveries & effects.</title> <source>Journal of Applied Behavior Analysis</source> <volume>25</volume> <pages>51-57</pages> </reference> XML my $twig = new XML::Twig(); $twig->parse($xml);
The parser dies with this error message -- not well-formed at line 8, column 50, byte 198 at C:/Perl/site/lib/XML/Parser.pm line 168 Now I know where the parser died, and a quick examination of the XML turns up a bare & which is a malformed entity. I would like to be able to correct this error as while the document is parsing, however I can't seem to find an option in either the XML::Parser or XML::Twig man pages to allow me to handle the error or at least report the error and continue parsing the document so I can gather all the problems with the document in one pass. Am I using the right tool for the job with XML::Twig? Should I write some sort of preprocessor or filter to fix these problems before passing the data to XML::Twig? Has someone already written a module to do this? Any advice will be appreciated. I would rather not get into the XML parser writting business if I don't have to(see On XML parsing).

--
Coyote


In reply to Dealing with Malformed XML by Coyote

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.