我是Xpath
的新手,我正在按照教程在xml文件中搜索一些目标数据,并根据下面发布的xml文件编写了以下代码。并且如下面的发布结果所示,我得到了一个nodeList,其中填充了四个null元素,我希望NodeList可以填充四封电子邮件,因为我的搜索表达式是
String expression1 = "/Employees//Employee[position()<=4]//email";
请让我知道为什么我会得到四个空元素
代码:
public static void main(String[] args) throws FileNotFoundException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = builder.parse(new FileInputStream("c:\\xml0.xml"));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression1 = "/Employees//Employee[position()<=4]//email";
String email = xPath.compile(expression1).evaluate(document);
System.out.println(email);
Node node = (Node) xPath.compile(expression1).evaluate(document, XPathConstants.NODE);
NodeList nodeList = (NodeList) xPath.compile(expression1).evaluate(document, XPathConstants.NODESET);
System.out.println(nodeList.getLength());
System.out.println(nodeList.item(0));
System.out.println(nodeList.item(1));
System.out.println(nodeList.item(2));
System.out.println(nodeList.item(3));
}
XML :
<?xml version="1.0"?>
<Employees>
<Employee emplid="1111" type="admin">
<firstname>John</firstname>
<lastname>Watson</lastname>
<age>30</age>
<email>johnwatson@sh.com</email>
</Employee>
<Employee emplid="2222" type="admin">
<firstname>Sherlock</firstname>
<lastname>Homes</lastname>
<age>32</age>
<email>sherlock@sh.com</email>
</Employee>
<Employee emplid="4444" type="user">
<firstname>Jim</firstname>
<lastname>Moriarty</lastname>
<age>52</age>
<email>jim@sh00.com</email>
</Employee>
<Employee emplid="4444" type="user">
<firstname>Jim</firstname>
<lastname>Moriarty</lastname>
<age>52</age>
<email>jim@sh33.com</email>
</Employee>
<Employee emplid="5555" type="admin">
<firstname>Mycroft</firstname>
<lastname>Holmes</lastname>
<age>41</age>
<email>mycroft@sh.com</email>
</Employee>
</Employees>
结果:
johnwatson@sh.com
4
[email: null]
[email: null]
[email: null]
[email: null]
答案 0 :(得分:2)
首先,您不需要&#34; /员工//员工[position()&lt; = 4] //电子邮件&#34;在这个XML中。 &#39; //&#39;表示任何后代,但在此XML中,Employee是Employees的直接子项,这意味着简单的&#39; /&#39;会做的。
要清除输出不为null。输出是节点元素&#39;电子邮件&#39;,这毕竟是您所要求的。
要获取您需要使用的文本值: &#34; /雇员/雇员[位置()&LT; = 4] /电子邮件/文本()&#34;
答案 1 :(得分:1)
我认为你在表达中错过了/text()
。尝试将其更改为
String expression1 = "/Employees//Employee[position()<=5]/email/text()";
否则,代码对我来说很好