XML解析器 - XML的XPath使用Java

问题描述:

<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

我编辑了我的答案,因为你编辑了你的帖子。

试试:

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

在你的情况,你并不需要检索的节点。直接从xpath评估中获得String值。