是xml文件
<priority-claims>
<priority-claim sequence="1" kind="national">
<document-id document-id-type="maindoc">
<doc-number>FD0297663</doc-number>
<date>20070403</date>
</document-id>
</priority-claim>
<priority-claim sequence="2" kind="national">
<document-id document-id-type="maindoc">
<doc-number>FD0745459P</doc-number>
<date>20060424</date>
</document-id>
</priority-claim>
</priority-claims>
我的预期条件:
1.如何获得所有节点值(即FD0297663, 20070403 and FD0745459P,20060424
)
2.its可以是单个(即Priority -claim标签)或多于一个级别
我的现有代码仅获得第一级值
String priorityNumber = xPath.compile("//priority-claim//doc-number").evaluate(xmlDocument);
String priorityDate = xPath.compile("//priority-claim//date").evaluate(xmlDocument);
答案 0 :(得分:1)
以下是一个工作示例:
/priority-claims/priority-claim/document-id/doc-number/text()
)使用NodeList
NodeList priorityNumbers = (NodeList) xPath.compile("/priority-claims/priority-claim/document-id/doc-number/text()").evaluate(xmlDocument, XPathConstants.NODESET);
NodeList priorityDates = (NodeList) xPath.compile("/priority-claims/priority-claim/document-id/date/text()").evaluate(xmlDocument,XPathConstants.NODESET);
for(int i=0; i<priorityNumbers.getLength();i++){
System.out.println(priorityNumbers.item(i).getNodeValue());
}
for(int i=0; i<priorityDates.getLength();i++){
System.out.println(priorityDates.item(i).getNodeValue());
}
以下是与具有可运行版本的要点的链接:https://gist.github.com/rparree/1c7eb8e9ca928b98418fdb167a2096a3