2016-08-18 140 views
1
<document-instance system="abc.org" number-of-pages="12" desc="FullDocument" link="A1/fullimage"> 
<document-instance system="abc.org" number-of-pages="6" desc="Drawing" link="A1/thumbnail"> 
<document-instance system="abc.org" number-of-pages="1" desc="FirstPage" link="PA/firstpage"> 

從上面的XML與條件編譯我想提取number-of-pages計數如果DESC = FullDocumentXML解析器 - XML的XPath使用Java

下面是我得到的頁數,而不管代碼 desc值,但我需要包括條件如何?

String pageCount = "//document-instance/@number-of-pages"; 
Node node = (Node) xPath.compile(pageCount).evaluate(xmlDocument, XPathConstants.NODE); 
String url = node.getTextContent(); 
+0

您需要正確選擇'document-instance':// document-instance [@ desc =「FullDocument」]'。 –

+0

你想檢索計數? – davidxxx

+0

@david:是隻檢索那個計數 – Prabu

回答

2

我編輯了我的答案,因爲你編輯了你的帖子。

試試:

String pageCountPath = "//document-instance[@desc='FullDocument']/@number-of-pages"; 
String pageCountValue = (String) xPath.compile(pageCountPath).evaluate(xmlDocument, XPathConstants.STRING); 

在你的情況,你並不需要檢索的節點。直接從xpath評估中獲得String值。