簡體   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>

我想從上面的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());
                }

但是它提取了所有三個節點值( country, doc-number and date ),但是我只需要兩個節點值( country and date ),在for循環中我應該如何傳遞請求的節點值?

選擇一個document-id節點后, //運算符將選擇其所有后代,
然后text()將它們中的每個都轉換為字符串。 如果您只想處理某些下降節點,只需列出它們(構建一個明確的子元素序列)。
您也可以擺脫昂貴的(和多余的了!)的//運營商。
嘗試將查詢替換為
“ //家庭成員/出版物參考/文檔ID [@ document-id-type = \\” docdb \\“] /(國家/日期)/ text()”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM