Prabu
Prabu

Reputation: 3728

xml parser - XML xpath compile with condition using 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">

from the above xml i want to extract number-of-pages count if the desc = FullDocument

below are the code i'm getting the number of pages irrespective of desc value, but i need to include the condition how?

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

Upvotes: 1

Views: 109

Answers (1)

davidxxx
davidxxx

Reputation: 131456

I edited my answer because you edited your post.

Try it :

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

In your case, you don't need to retrieve the node. Get directly the String value from the xpath evaluation.

Upvotes: 2

Related Questions