Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to access data from arrays with XML::Simple

by ccarden (Monk)
on Oct 14, 2003 at 20:54 UTC ( [id://299259]=perlquestion: print w/replies, xml ) Need Help??

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

I began a similar thread on this topic, but this seems more to be a fundamental lack of understanding of arrays in Perl. It has me more than scratching my head. I've read through the perldocs and am missing something.

I've never pretended to be a great Perl programmer, but I really shouldn't be falling down on points like this. After 6 or 7 years of this, you'd think I'd actually know something. Maybe I should take up singing in a punk band.

Could someone direct me to documentation that will help me understand how to solve the following ...

The code:
#!/usr/bin/perl -w use XML::Simple; # qw(:strict); my $ref = XMLin("xmlTest3.xml", forcearray=>1, keyattr=>{wrapper=>"nam +e"}); my $wrapName = "testName 1.0.3"; print "$ref->{wrapper}->{$wrapName}->{script}\n"; print "$ref->{wrapper}->{$wrapName}->{envVar}->[0]->{value}\n"; # How do I enumerate the contents of the tags such as envVar and parm? # The following only gives me an address to the array. foreach my $pName ($ref->{wrapper}->{$wrapName}->{envVar}) { print $pName; }
The data:
<config> <wrapper name="testName 1.0.3" script="/usr/people/ltorvalds/wrapp +er.pl" app="/usr/bin/testApp.exe"> <envVar name="TMPDIR" value="/usr/tmp"/> <envVar name="TEMPDIR" value="/usr/tmp"/> <parm name="pad" value="4"/> <parm name="me" value="0"/> <parm name="mf" value="0"/> <parm name="s" value="\$startFrame"/> <parm name="e" value="\$endFrame"/> <parm name="b" value="\$byFrame"/> <parm name="im" value="\$cacheDir\$qmJob.\$suffix"/> <parm name="of" value="\$fileExt"/> </wrapper> <wrapper name="testName2" script="/usr/people/wgates/wrapper2.pl" +engine="/usr/bin/testApp2.exe"> <envVar name="MAYA_LOCATION" value="\$appPath"/> <envVar name="MAYA_SCRIPT_PATH" value="/data/render_common/scr +ipts"/> <envVar name="TMPDIR" value="/usr/tmp"/> <envVar name="TEMPDIR" value="/usr/tmp"/> <parm name="pad" value="4"/> <parm name="me" value="0"/> <parm name="mf" value="0"/> <parm name="s" value="\$startFrame"/> <parm name="e" value="\$endFrame"/> <parm name="b" value="\$byFrame"/> <parm name="im" value="\$cacheDir\$qmJob.\$suffix"/> <parm name="of" value="\$fileExt"/> </wrapper> </config>
Thanks for any input.

Replies are listed 'Best First'.
Re: How to access data from arrays with XML::Simple
by jeffa (Bishop) on Oct 14, 2003 at 21:22 UTC
    When dealing with complex datastructures, always bring Data::Dumper along for the ride. Here is some code that enumurates through the param elements of the "testName 1.0.3" <wrapper> tag:
    use strict; use warnings; use XML::Simple; use Data::Dumper; my $ref = XMLin(\*DATA, forcearray=>1, keyattr=>{wrapper=>"name"}); # feel free to uncomment these Dumps #print Dumper $ref; for (@{$ref->{wrapper}{'testName 1.0.3'}{parm}}) { #print Dumper $_; while(my ($k,$v) = each %$_) { print "$k => $v\n"; } } __DATA__ <config> ... rest of XML ...
    Be sure and check out tye's excellent References quick reference also.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-24 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found