Java Xpath 查询 [英] Java Xpath query
本文介绍了Java Xpath 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Java Xpath 将第二个c"标签 d 和 f 的值设为 2
How can I use Java Xpath to get the value of 2nd "c" tag d and f as 2
<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
How can I use Java Xpath to get the value of 2nd "c" tag d and f as 2
推荐答案
使用:
//a/b/c[@type='lol'][2]/d/text()
这篇关于Java Xpath 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文