2011-05-23 55 views
-1

如何使用屬性值解析xml元素。我使用Dom來解析XML文件。我不確定解析只有屬性值的xml元素。這是可能的DOM .. ??如何使用屬性值解析xml元素

+0

可能的重複[使用屬性獲取元素](http://stackoverflow.com/questions/6093121/getting-element-using-attribute) – 2011-05-23 06:46:30

+0

實際猜測的問題(包括重複):「如何查找/選擇具有給定屬性值的XML文檔中的所有元素「... – 2011-05-23 07:14:14

回答

0

以下XPath表達式將選擇給attibute /屬性值對所有元素:

//*[@foo="bar"] 

實施例:

<root> 
<grand foo="bar"> <!-- selected --> 
    <child foo="nobar"/> 
</grand> 
<grand foo="nobar"> 
    <child foo="bar"/> <!-- selected --> 
</grand> 
</root> 

這裏的一個tutorial for the Java XPath API (jaxp)。它有一些片段顯示如何使用它。