2011-11-16 108 views
0

我有以下的XML格式如何通过id获取节点?

<locale id='us'> 
items here 
</locale> 

我如何获得它的ID的区域设置的节点?

我有以下代码

$xml = simplexml_load_file("config.xml"); 
$nodes = $xml->xpath('*[id = "us"]'); 

,但我想这不是正确的方式

回答

1

使用@轴描述符指一个属性:

*[@id='us'] 

如果你想通过可以出现在文档中任何位置的ID获取元素,然后使用:

//*[@id='us'] 
1
$xml = simplexml_load_file("config.xml"); 
$nodes = $xml->xpath("*[@id='us']");