【发布时间】:2012-08-07 10:59:15
【问题描述】:
好的,我有一些基本的 XML 格式如下:
<application>
<authentication>
<id>26</id>
<key>gabe</key>
</authentication>
<home>
<address>443 Pacific Avenue</address>
<city>North Las Vegas</city>
<state>NV</state>
<zip>89084</zip>
</home>
</application>
我正在使用simplexml_load_string()将上面的XML加载到一个变量中,如下:
$xml = simplexml_load_string($xml_string);
我想提取第二个节点的名称/值对,例如,我想忽略<authentication> 和<home> 节点。我只对这些一级节点内的子节点感兴趣:
- 身份证
- 键
- 地址
- 城市
- 状态
- 压缩包
所以我正在寻找一个 foreach 循环,它将提取出上述 6 个名称/值对,但忽略“较低级别”的名称/值对。以下代码仅打印 <authentication> 和 <home> 节点的名称/值对(我想忽略):
foreach($xml->children() as $value) {
$name = chop($value->getName());
print "$name = $value";
}
有人可以帮我写出仅提取上述 6 个节点的名称/值对的代码吗?
【问题讨论】:
标签: php xml loops simplexml extract