简体   繁体   中英

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

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?

Once you selected a document-id node, the // operator selects its ALL descendants,
then text() converts each of them into a string. 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()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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