java 使用 Xpath 检查 Xml 文件中是否存在特定节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11650690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if particular node exists in Xml file using Xpath
提问by thetna
I would like to check is code = "ABC"
exists in my xml file using xPath.Can you please suggest me some methods for it?
我想code = "ABC"
使用 xPath检查我的 xml 文件中是否存在。你能给我建议一些方法吗?
<metadata>
<codes class = "class1">
<code code = "ABC">
<detail "blah blah"/>
</code>
</codes>
<codes class = "class2">
<code code = "123">
<detail "blah blah"/>
</code>
</codes>
</metadata>
[EDIT] I did following. It reuturns null.
[编辑] 我做了以下。它返回 null。
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xPath.compile("//codes/code[@ code ='ABC']");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("nodes: "+ nodes.item(i).getNodeValue());
}
回答by Favonius
I don't know how you tested your code because <detail "blah blah"/>
is an incorrect xml construct it should be <detail x="blah blah"/>
i.e. a name-valuepair !!
我不知道你<detail "blah blah"/>
是如何测试你的代码的,因为它是一个不正确的 xml 构造,它应该是<detail x="blah blah"/>
一个名称-值对!!
For the XPath Expression "//codes/code[@ code ='ABC']"
the nodes.item(i).getNodeValue())
is going to be null
because it will return an Element. See the below Javadoc comment:
对于 XPath 表达式"//codes/code[@ code ='ABC']"
,这将nodes.item(i).getNodeValue())
是null
因为它将返回一个元素。请参阅下面的 Javadoc 评论:
A working sample:
A working sample:
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Test
{
public static void main(String[] args) throws Exception
{
Document doc = getDoc();
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println("Have I found anything? " + (nodes.getLength() > 0 ? "Yes": "No"));
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("nodes: "+ nodes.item(i).getNodeValue());
}
}
private static Document getDoc()
{
String xml = "<metadata>"+
"<codes class = 'class1'>"+
"<code code='ABC'>"+
"<detail x='blah blah'/>"+
"</code>"+
"</codes>"+
"<codes class = 'class2'>"+
"<code code = '123'>"+
"<detail x='blah blah'/>"+
"</code>"+
"</codes>"+
"</metadata>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(new ByteArrayInputStream(xml.getBytes()));
return dom;
}catch(Exception pce) {
pce.printStackTrace();
}
return null;
}
}