好的,我有一些基本的 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>
and<home>
节点。我只对这些一级节点内的子节点感兴趣:
- ID
- 钥匙
- 地址
- 城市
- 状态
- 压缩
所以我正在寻找一个 foreach 循环,它将提取上述 6 个名称/值对,但忽略“较低级别”的名称/值对。<authentication>
以下代码仅打印and节点的名称/值对<home>
(我想忽略):
foreach($xml->children() as $value) {
$name = chop($value->getName());
print "$name = $value";
}
有人可以帮我解决只提取上述 6 个节点的名称/值对的代码吗?