As I said, need to stick with XML::Parser!

Not exactly. You seem to have convinced yourself that 1- you need to only use XML::Parser, 2- the Tree style is the simplest way to get what you want. It seems to me that 1 is false lazyness, and 2 is just misguided.

Learning how to use pure-perl modules, even on a machine where you don't have admin rights, would make it easier for you to write not only this piece of code, but also the next ones.

Your problem seems really adapted to a stream processing, whether it's using XML::Parser or an other module. Your code would be much more resistant to changes in the XML structure in the future: in your example [1]->[4]->[4]->[2] is effectively the hardcoded (and some would say obfuscated) path to your target element. If you don't want to hardcode it, you will end up re-writing code that's already written in the likes of XML::Twig, XML::XPath, XML::Rules... Meanwhile with a stream processing you would just process the firstname element, and leave the rest as is, thus you would be able to apply your code even if the input XML changes, as long as it still includes a firstname element.

That said, it's your code, you do what you want, just realize that you will get more help if you follow the general advice of using a better tool for the task.


In reply to Re^3: Trees in XML by mirod
in thread Trees in XML by Anonymous Monk

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.