Greetings fellow monks,

I've just started working on a project at work that requires the parsing of XML.  I've never used an XML parser before this week.

After considering XML::Twig, I decided to focus on XML::Simple instead.  For one thing, it seemed easier to delve into XML parsing with.  For another, it's clear that the XML this project involves is really basic stuff; no arrays of data, nor even any nested data after the root.  (Although I'm aware that I may be being naive).

I've read through the module documentation a few times, and figured out how to preserve the root of the XML with 'KeepRoot' => 1, as well as what other arguments are required if I want to go with the qw{ :strict } option.

On the very first set of actual XML data that I tried, there happened to be an error in the input.  Here's my example program with simplified test XML at the end that still exhibits the error:

#!/usr/bin/perl -w ############### ## Libraries ## ############### use strict; use warnings; use XML::Simple qw{ :strict }; ################## ## User-defined ## ################## my $h_xml_args = { 'forcearray' => [ ], 'keyattr' => [ ], 'KeepRoot' => 1, }; ################## ## Main program ## ################## chomp(my @xml = <DATA>); my $xml = join("\n", @xml); # Read and display the input XML print "[Input XML]\n"; print "-" x 79, "\n"; for (my $i = 0; $i < @xml; $i++) { printf " %3d. %s\n", $i+1, $xml[$i]; } print "-" x 79, "\n\n"; # Parse the XML with XML::Simple my $h_xml = eval { XMLin($xml, %$h_xml_args) }; if ($@) { die "Error while parsing XML:\n$@\n"; } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE MSG PUBLIC "SYSTEM" "MESSAGE.dtd"> <ROOT> <TYPE>notification</TYPE> <IDENT>1972308645</IDENT> <STATE>processed</STATE> <HEADER>Example of XML::Simple</HEADER> <ERROR>The error: no closing "/ERROR" tag<ERROR> <MORE_INFO>More info</MORE_INFO> <STILL_MORE>Still more info</STILL_MORE> <AND_SO_ON>And so on ...</AND_SO_ON> </ROOT>

Note that the 8th line of XML is incorrect, as the closing tag is missing its slash "/" character:

<ERROR>The error: no closing "/ERROR" tag<ERROR>

The error this program gives me back is:

Error while parsing XML: mismatched tag at line 12, column 2, byte 377 at /usr/lib/perl5/site_p +erl/5.8.8/ i386-linux-thread-multi/XML/Parser.pm line 187

which seems awfully non-specific.  I had to visually scan through the XML to see that the error was really on line 8 of the XML, not on line 12 (the final line).

In actuality, the XML will be much longer, of course, and it would be nice to provide something along the lines of:

Error while parsing XML: The tag <ERROR> (line 8) was never closed.

or some such.

My situation is very like an XML beginner's confusion.  A Google search for the error message I got didn't seem to help.  Can anyone enlighten me as to the best way to pinpoint exactly where the error occurred?  Is there something in XML::Simple that I've overlooked?  Should I be using a different module?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to XML::Simple giving a non-specific error by liverpole

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.