简体   繁体   English

xpath使用Java编译-具有特定节点值的NodeList

[英]xpath compile using java - NodeList with particular node value

<data xmlns:fsd="abc.org" xmlns:xlink="http://www.w3.org/1999/xlink">
<meta name="elapsed-time" value="46" />
<org-family>
<family-member id="5">
<publication-reference>
<document-id document-id-type="docdb">
<country>US</country>
<doc-number>3056228</doc-number>
<date>20160817</date>
</document-id>
</publication-reference>
</family-member>
<family-member id="2">
<publication-reference>
<document-id document-id-type="docdb">
<country>US</country>
<doc-number>2013315173</doc-number>
<date>20150430</date>
</document-id>
</publication-reference>
</family-member>
</org-family>
</data>

From the above xml i want to extract country and date node value, below are my java code 我想从上面的xml中提取国家和日期节点值,下面是我的java代码

NodeList familyMembers = (NodeList) xPath.compile("//family-member//publication-reference//document-id[@document-id-type=\"docdb\"]//text()").evaluate(xmlDocument,XPathConstants.NODESET);

 ArrayList mainFamily = new ArrayList();
                for (int i = 0; i < familyMembers.getLength(); i++) {
                    mainFamily.add(familyMembers.item(i).getNodeValue());
                }

but its extract all the three node value ( country, doc-number and date ), but i need only the two node value ( country and date ), in the for loop how should i pass the requested node value? 但是它提取了所有三个节点值( country, doc-number and date ),但是我只需要两个节点值( country and date ),在for循环中我应该如何传递请求的节点值?

Once you selected a document-id node, the // operator selects its ALL descendants, 选择一个document-id节点后, //运算符将选择其所有后代,
then text() converts each of them into a string. 然后text()将它们中的每个都转换为字符串。 If you want to process only some of the descentant nodes, just list them (build an explicit sequence of sub-elements). 如果您只想处理某些下降节点,只需列出它们(构建一个明确的子元素序列)。
You can also get rid of expensive (and superfluous here!) // operators. 您也可以摆脱昂贵的(和多余的了!)的//运营商。
Try replacing the query with 尝试将查询替换为
"//family-member/publication-reference/document-id[@document-id-type=\\"docdb\\"]/(country, date)/text()" “ //家庭成员/出版物参考/文档ID [@ document-id-type = \\” docdb \\“] /(国家/日期)/ text()”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM