I am using XML::Simple and I have the following XML structure in a variable $xmldata which I need to access through Perl code.
我正在使用XML::Simple,并且在变量$xmldata中具有以下XML结构,我需要通过Perl代码访问它。
<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 :
我可以使用以下代码访问a和b的值:
$aval = $xmldata->{a}[0];
$bval = $xmldata->{b}[0] ;
Now, my question is: how can I get the value of say, d2 ?
现在,我的问题是:我如何得到这个值,d2 ?
1 个解决方案
#1
5
Given what you have above, I assume that you have the ForceArray flag enabled. Nested keys are stored as hashes of hashes using references.
根据上面的内容,我假设您已经启用了ForceArray标志。使用引用将嵌套键存储为散列。
So, to access 'd2' you would need to use:
因此,要访问“d2”你需要使用:
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.
显然,你越往深处走,它就越可怕。这就是我几乎总是建议使用XML::LibXML和XPath而不是XML::Simple的原因之一。XML:简单迅速变得不简单。XML:简单的文档解释了各种选项如何影响这个布局。
Data::Dumper is invaluable when you want to take a look at how the data are laid out.
数据:当你想要查看数据的布局时,Dumper是无价的。
#1
5
Given what you have above, I assume that you have the ForceArray flag enabled. Nested keys are stored as hashes of hashes using references.
根据上面的内容,我假设您已经启用了ForceArray标志。使用引用将嵌套键存储为散列。
So, to access 'd2' you would need to use:
因此,要访问“d2”你需要使用:
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.
显然,你越往深处走,它就越可怕。这就是我几乎总是建议使用XML::LibXML和XPath而不是XML::Simple的原因之一。XML:简单迅速变得不简单。XML:简单的文档解释了各种选项如何影响这个布局。
Data::Dumper is invaluable when you want to take a look at how the data are laid out.
数据:当你想要查看数据的布局时,Dumper是无价的。