XML::Parser
is useful for testing XML output, mainly because it's in the standard Perl distribution and it's stable. However, I ran into a problem with it yesterday that cost me some time.
Here's some (simplified) code
$parser = new XML::Parser(Style => 'Tree'); $parser->parse($file_name);
Those who know the module well will have spotted the error already. I was faced with this error message:
not well-formed (invalid token) at line 1, column 0, byte 0 at /usr/lib/perl5/[...]/XML/Parser.pm line 187
I tried making my XML output smaller and smaller until it was just one self-closing element. Still I was getting the invalid token error.
Turned out I was calling the wrong method on the parser. A lot of Perl methods are pretty liberal about what they accept, perhaps too liberal at times. $parser->parse
accepts a file handle or a string, but not a file name. My fault for not reading the documentation carefully, because it says, right at the top, $p1->parsefile('REC-xml-19980210.xml')
.
Perhaps not literally the least helpful error message ever. Certainly misleading for anyone unfortunate enough to run into it.