Hello Kind Monks:

After some more hacking and reading, found my errors

This code will do the trick:

my @el = $posNode->getElementsByTagName("itemName", 0); print scalar @el; print " " . $el[0]->getNodeName() . " " . nodeTypeText($el[0]- +>getNodeType()) ." \n" ;

bottom line getElementsByTagName works just fine.

I have been tasked with parsing a XML document that contains inventory info. The root level is <inventory> and then within inventory there are the inventory items, Ie <item1>, <item2>, <item3> ....... Within each inventory item there are many elements that describe that particular item. Ie <itemName>jeans501</itemName>, <itemId>j501ab</itemId>, <countryOrgin>mexico</countryOrgin>, <style>, etc...

Using XML::DOM I have written code that will rip through the XML and dump the elements one-by-one.

Here is my issue.

Given the node <item1> how do I ask for the node named "itemName" and ask for the node named "countryOrgin"? I wan t to ask for the info I need rather than ripping through the full item and then grapping the data. The way a database recordset would work.

I think that would be prettier code.

Thanks for considering my issue.

kd


In reply to XML::DOM get Element By Name by kevind0718

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.