【发布时间】:2016-04-08 09:28:14
【问题描述】:
我正在读取一个 xml 文件,
$myxml = XMLin("$configfile");
当我使用 Dumper (print Dumper($myxml);) 打印它时,我得到了这个......
$VAR1 = {
'Timeout' => 5,
'Roots' => {
'Root' => [
{
'Name' => 'Sales',
'Level' => 'Indeterminate',
'Profiles' => {
'Profile' => [
{
'Name' => 'Bill',
'Age' => '50',
'Status' => Active
},
{
'Name' => 'Bob',
'Age' => '24',
'Status' => Inactive
}
]
},
'Interval' => 'Order',
'Action' => 'Reject'
},
{
'Name' => 'User',
'Level' => 'Indeterminate',
'Profiles' => {
'Profile' => [
{
'Name' => 'User',
'Action' => 'Reject',
'User' => 'acount'
},
{
'Name' => 'Admin',
'Action' => 'Accept',
'User' => 'acount'
},
]
}
};
我想读取此哈希并获取所有非活动“状态”的值,或者获取“鲍勃的状态”..
{
'Name' => 'Bob',
'Age' => '24',
'Status' => Inactive
}
开始编辑:
所以要获取一个人的个人资料信息..
Dumper($myxml->{'Roots'}->{'Root'}[0]{'Profiles'}{'Profile'}[2]);
例如获取 Bob 的状态
if ($myxml->{'Roots'}->{'Root'}[0]{'Profiles'}{'Profile'}[1]{'Name'} eq "Bob") {
$status = $myxml->{'Roots'}->{'Root'}[0]{'Profiles'}{'Profile'}[1]{'Status'};
}
但是,如果 Bob 不在位置 [0] 中,我如何循环遍历此 xml,以便它会继续检查 {'Roots'}->{'Root'} 和 {'Profiles'}{'Profile'}和 [1]。双 foreach 循环?
结束编辑
我已经包含了一个 xml 的示例..
<Root Name="Sales" Level="Indeterminate" Profile="Order" Interval="Order" Action="Reject">
<Profiles>
<Profile Name="Bill" Age="50" Status=Active />
<Profile Name="Bob" Age="24" Status=InActive />
<Profile Name="Ben" Age="45" Status=Active />
</Profiles>
</Root>
产生这个:
$VAR1 = {
'Name' => 'Sales',
'Type' => 'Indeterminate',
'Profiles' => {
'Profile' => [
{
'Name' => 'Bill',
'Age' => '50',
'Status' => Active
},
{
'Name' => 'Bob',
'Age' => '24',
'Status' => InActive
},
{
'Name' => 'Ben',
'Age' => '45',
'Status' => Active
}
]
},
'Interval' => 'Order',
'Action' => 'Reject'
};
谢谢,
约翰。
【问题讨论】:
-
如果您给我们一些示例 XML,我们可以向您展示一个更简单的解决方案。我已经猜测了您的源 XML 是什么样子来说明这个概念的。
标签: perl hash data-dumper