我如何使用Java XPath來獲得第二屆「C」標記d和f值作爲2的Java Xpath查詢
<a>
<b>
<c type="lol">
<d>1</d>
<f>2</f>
</c>
<c type="lol">
<d>2</d>
<f>2</f>
</c>
<c type="h">
<d>v</d>
<f>d</f>
</c>
</b>
</a>
{
DocumentBuilderFactory dBFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dB = dBFactory.newDocumentBuilder();
Document doc = dB.parse(url);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr=null;
String text= null;
expr= xpath.compile("//a/b/c[@type='lol']/d/text()");// this gets the first value
text = (String) expr.evaluate(doc, XPathConstants.STRING);
}
我如何使用Java XPath來獲得第二屆「C」標籤的值d和f爲2
你的問題可以用一個或兩個的調整做 - 的XML ENDTAG是錯誤的,示例XPath說,「b」,而不是d。下面的好回答,雖然,我今天學到了一些東西;] – davidfrancis 2012-02-02 09:23:06