2011-11-16 163 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']");