在给定的 XML 文件中,我正在尝试使用XPath
Java 搜索字符串的存在。但是,即使字符串在那里,我的输出总是显示为否。希望这里的人能指出我可能做错了什么?
XML 文件:
<article>
<body>
<section>
<h1>intro1</h1>
<region>introd1</region>
<region>introd2</region>
</section>
<section>
<h1 class="pass">1 task objectives</h1>
<region>object1</region>
<region>object2</region>
</section>
<section>
<h1 class="pass">1 task objectives</h1>
<region>object1</region>
<region>This is the Perfect Word I am looking for</region>
</section>
</body>
</article>
在 Java 中,我试图检查是否存在"perfect"
这样的单词:
expr = xpath.compile("//article//body//section//region[contains(.,'perfect')]");
object result = expr.evaluate(doc,XPathConstants.NODESET);
NodeList nodes = (NodeList)result;
if (nodes.getLength() > 0) {
System.out.println("Found");
// do other stuff here
} else {
System.out.println("Not found");
}
当我运行它时,输出总是"Not Found"
. 知道我做错了什么吗?