<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 = FullDocument
下面是代码 我得到的页数与desc值无关,但我需要包括条件如何?
String pageCount = "//document-instance/@number-of-pages";
Node node = (Node) xPath.compile(pageCount).evaluate(xmlDocument, XPathConstants.NODE);
String url = node.getTextContent();
答案 0 :(得分:2)
我编辑了你的答案,因为你编辑了你的帖子。
试一试:
String pageCountPath = "//document-instance[@desc='FullDocument']/@number-of-pages";
String pageCountValue = (String) xPath.compile(pageCountPath).evaluate(xmlDocument, XPathConstants.STRING);
在您的情况下,您不需要检索节点。直接从String
评估中获取xpath
值。