Hi... i'm working with the XML::Parser module to implement a SAX handler. My XML snippet is:

<comp id="GWBC18827"> <nam type="preferred">NAD(P)H</nam> <nam type="alias">NAD(P) H</nam> <specific>GWBC41</specific> <specific>GWBC43</specific> </comp> <comp id="GWBC43"> <nam type="preferred">NADPH</nam> <nam type="alias">TPNH</nam> <cas>2646-71-1</cas> <for>C21H30N7O17P3</for> <gen>NAD(P)H</gen> <smi>[C@H]1(O[C@@H]([C@@H](O)[C@H]1OP(=O)([O-])[O-] +)COP(=O)(OP(=O)(OC[C@H]2[ C@@H](O)[C@@H](O)[C@@H](O2)N3C=C(CC=C3)C(=O)N)[O-])[O-])[N+]4=C5C(=NC4 +)C(=NC =N5)N</smi> <smi>NC(=O)C1=CN(C=CC1)[C@@H]2O[C@H](COP(=O)(O)OP(=O)(O)OC +[C@H]3O[C@H]([C@H] (OP(=O)(O)O)[C@@H]3O)n4cnc5c(N)ncnc45)[C@@H](O)[C@H]2O</smi> <ref name="EMP">C139</ref> <ref name="Klotho">KLM0000287</ref> <ref name="Kegg">C00005</ref> <ref name="brenda">no ref</ref> </comp> <comp id="GWBC41"> <nam type="preferred">NADH</nam> <nam type="alias">DPNH</nam> <for>C21H29N7O14P2</for> <gen>NAD(P)H</gen> <smi>[C@H]1(O[C@@H]([C@@H](O)[C@H]1O)COP(=O)(OP(=O) +(OC[C@H]2[C@@H](O)[C@@H]( O)[C@@H](O2)N3C=CCC(=C3)C(=O)N)[O-])[O-])[N+]4=C5C(=NC4)C(=NC=N5)N</sm +i> <ref name="EMP">C136</ref> <ref name="Klotho">KLM0000285</ref> <ref name="Kegg">C00004</ref> </comp>

... and the code I wrote to parse it is here:

use XML::Parser; # Variables my $xmlp; my $currentTag = ""; # Prototypes # Create new parser and set callbacks $xmlp = new XML::Parser(); $xmlp->setHandlers( Start => \&start, End => \&end, Char => \&cdata ); # Parse the file $xmlp->parsefile("../thes_ex.xml"); # Called when a tag is started sub start() { # extract variables my ($parser, $name, %attr) = @_; $currentTag = lc($name); print "start $currentTag\n"; } # Called when a tag is ended sub end() { # extract variables my ($parser, $name) = @_; $currentTag = lc($name); print "ended $currentTag\n"; # clear value of current tag $currentTag = ""; $cdata = ""; } # Called when CDATA section found sub cdata() { my ($parser, $data) = @_; print "cdata: $data\n"; }

Given the XML above the following error is given _after_ the first <comp>...</comp> block: "junk after document element, line 7".

Clearly this refers to my lack of having a single root node and as I don't generate the XML I have little control over this... so does anyone know how I can stop this error. I have tried putting in a root tag enclosing the file and this fixes the problem - not an option though in my problem.

Best wishes, Arun


In reply to Parsing with SAX an XML Document with no Root Node by arunhorne

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.