Reputation:
I am using XML::Simple and I have the following XML structure in a variable $xmldata which I need to access through Perl code.
<root>
<a>sfghs</a>
<b>agaga</b>
<c>
<c1>sgsfs</c1>
<c2>sgsrsh</c2>
</c>
<d>
<d1>agaga</d1>
<d2>asgsg</d2>
</d>
</root>
I can access the value of a and b by using the following code :
$aval = $xmldata->{a}[0];
$bval = $xmldata->{b}[0] ;
Now, my question is: how can I get the value of say, d2 ?
Upvotes: 1
Views: 855
Reputation: 7143
Given what you have above, I assume that you have the ForceArray flag enabled. Nested keys are stored as hashes of hashes using references.
So, to access 'd2' you would need to use:
my $d2val = $xmldata->{d}[0]->{d2}[0];
(or my preference)
my $d2val = $xmldata->{d}->[0]->{d2}->[0];
(because it makes the deref obvious)
Obviously, the deeper you go the scarier this gets. That's one of the reasons I almost always suggest XML::LibXML and XPath instead of XML::Simple. XML::Simple rapidly becomes not simple. XML::Simple docs explain how various options can affect this layout.
Data::Dumper is invaluable when you want to take a look at how the data are laid out.
Upvotes: 5