http://www.perlmonks.org?node_id=979682


in reply to Re: XML::Twig help
in thread XML::Twig help

Okay, it didn't even slightly work though. Also I changed the data to make it easier for you guys to help.

foreach my $info ($root->children('data')){ my $INTEREST2 = $drug->first_child_text('groups/group'); print "$INTEREST2"; print "\n"; }

I have no idea how to capture INTEREST1 as it is within the tag. What I've written here didn't work as groups/group is not valid.

Replies are listed 'Best First'.
Re^3: XML::Twig help
by mirod (Canon) on Jul 03, 2012 at 13:43 UTC

    There are several ways to get the data. first_child only goes down 1 step in the tree. To get the text, you would need $drug->first_child('groups')->field( 'group'), or $drug->next_elt( 'groups')->text or $drug->findvalue( 'groups/group'). INTEREST1 is an attribute, so you would get it with $drug->att( 'type').

    There is probably a problem though either with your simplified data or with the code. You have te tags in your example as data1 and data2. If this is really the case, then you won't access them with $root->children('data'), since the tags are NOT data. So either the tags are data, or you have to get them using $root->children.

      Thank you, that is very helpful. And yes the tags data1 data2 should just be data, that was my mistake :)

Re^3: XML::Twig help
by Anonymous Monk on Jul 03, 2012 at 13:01 UTC