Java Xpath 查询
如何使用 Java Xpath 获取第二个“c”标签 d 和 f 的值为 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">
<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);
}
How can I use Java Xpath to get the value of 2nd "c" tag d and f as 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
Use: