This is why XML::Simple is anything but simple. Given:

use XML::Simple; use Data::Dumper; print Dumper XMLin do { local $/ = <DATA> }; __DATA__ <log> <entry> <date>2012-01-08</date> <text>Test</text> </entry> <entry> <date>2012-01-09</date> <text>Test</text> <text>Additional information</text> </entry> </log>

... as you can see, for each entry, the value for the "text" key is sometimes a string and sometimes an arrayref. You need to either test the value of "text" with ref to see if it's an arrayref, or pass additional options to XMLin to force it to always be an array. Once you've got a few potentially duplicable elements, this really starts to eliminate the supposed simplicity of XML::Simple.

Better to start straight out with something like XML::LibXML, which might present a more complicated API to begin with, it's at least consistent. Even if your XML gets more complex, your script doesn't have to.


In reply to Re: XML parsing by tobyink
in thread XML parsing by asa

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.