omegaweaponZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a question in how to handle parsing out node values that have the same title. Right now when I use Lib XML it finds the nodes, but dumps them altogether as one string. I need to be able to separate them out uniquely. Any good/easy way to do that? Here's an example:
<parent_node> <node1><child_node>stuff_I_can_get</child_node></node1> <node2><child_node_a>stuff_I_want_1</child_node_a></node2> <node2><child_node_a>stuff_I_want_2</child_node_a></node2> </parent_node>
So I can grab node1's value of "stuff I can get" without issue, but when I grab node2's value under "child node a", since both are called node2 and child node a, both ""stuff_I_want_1" and "stuff_I_want_2" come back together as one string and would return as "stuff_I_want_1stuff_I_want2" ....my code is:
foreach $test($dom->findnodes('/parent_node')) { $mystring = $test->findnodes('./node2/child_node_a'); }
So I need a way to identify both nodes uniquely somehow even though they are called the exact same thing but have two different values

Replies are listed 'Best First'.
Re: Duplicate XML Node Question
by choroba (Cardinal) on Sep 18, 2014 at 17:14 UTC
    findnodes returns a node list. You have to iterate over it the same way you already did for the parent node:
    for my $test ($xml->findnodes('/parent_node')) { for my $mystring ($test->findnodes('node2/child_node_a')) { print $mystring, "\n"; } }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Makes perfect sense, that's exactly what I needed to do. Thanks!