Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

XML::Twig help

by yesitsjess (Initiate)
on Jul 03, 2012 at 12:44 UTC ( #979677=perlquestion: print w/ replies, xml ) Need Help??
yesitsjess has asked for the wisdom of the Perl Monks concerning the following question:

<?xml version="1.0" encoding="UTF-8"?> <somerubbish> <data1 type="INTEREST1" somerubbish> <groups> <group>INTEREST2</group> </groups> <targets> <target position="1" partner="INTEREST3"> <known-action>INTEREST4</known-action> </target> </targets> </data1> <data2 ....(etc)

How can I capture INTEREST1,2,3&4? I have been trying for a while now, read several help pages, and starting to feel it would be far easier just to use regexes. Can anyone help please?

Comment on XML::Twig help
Download Code
Re: XML::Twig help
by Anonymous Monk on Jul 03, 2012 at 12:47 UTC

    How can I capture INTEREST1,2,3&4? I have been trying for a while now, read several help pages, and starting to feel it would be far easier just to use regexes. Can anyone help please?

    Show what you tried, and explain how it failed

      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.

        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.

Re: XML::Twig help
by Ransom (Beadle) on Jul 03, 2012 at 13:02 UTC

    You can use the cpan module XML::Simple to read in the XML and create a hash. It looks similar to:

    my $fields = XMLin('fields.xml') or die "Cannot read fields.xml\n";

    Then you can use Data::Dumper or something similar to print the hash. Then it's as easy as following the key/value paris until you are at the data you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://979677]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-12-26 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls