Recently I wrote a piece of code to make human readable digest versions of XML logfiles that are generated from the Kana customer service email server.

Once I was done I started testing my code & voila I was bitten! By parsing XML by hand I forgot that parameters could come in any order & so my code broke when I passed in a logfile generated from Kana's email-a-log function, in which the parameters for a certain tag are given in a different order.

So... I set out to learn how to use XML::Parser & stop trying to reinvent the wheel... DUH!

It works! And (not so)incredibly it's almost 100% faster than the routine I wrote.

Now I have a different problem.

I find that when I call $p->parsefile() I'm getting the first line of the XML files printed to the screen. (Assumably to STDERR) For example:

<?xml:stylesheet type="text/xsl" href="log.xsl"?>

Now I assume that this is going to STDERR because I found that when I put in a die, parsefile would fail. However, when I comment it out, it continues to function properly...Like so:

my $p=new XML::Parser(Style=>'Stream') or die ("Couldn't make new Parser\n$GOODBYE\n"); $p->parsefile($logfile, ErrorContext => 0) ; #or die ("Couldn't invoke parsefile in XML::Parser...\n$GOODBY +E\n");

Why is it doing this & how do I handle it more gracefully?


I went so far as to modify StartTag to try to handle it, but that didn't do anything either...

sub StartTag { my ($expat,$eltype)=@_; if ($eltype eq "log") { $version = $_{version}; $datestamp = $_{date}; } elsif ($eltype eq "node") { } elsif ($eltype eq "component") { } elsif ($eltype eq "?xml:stylesheet") { } elsif ($eltype eq "m") { $p[i] = $_{p}; $component_type[$i] = $_{c}; $datetime[$i] = $_{d}; $u[$i] = $_{u}; $sequential_id[$i] = $_{i}; $node_id[$i] = $_{n}; $severity[$i] = $_{s}; $source[$i] = $_{t}; } else { die "invalid element: $eltype"; } }

So what am I doing wrong?



Wait! This isn't a Parachute, this is a Backpack!

In reply to XML::Parser trouble by gregor42

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.