我正在使用以下 XPath 查询来搜索一本书作者的姓名,并在与作者匹配时返回书名。
String rawXPath = String.format("//book[author= '%s']/bookname/text()", authorBook);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr
= xpath.compile(rawXPath);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
如何修改搜索以便我可以在内容...../content(node name) 中搜索特定词。
示例:xml 内容变量中的字符串:“这本书包含了我们祖先的光荣和历史。它对我们日常生活的影响是巨大的。”
现在我想搜索“daily”这个词。如果它每天匹配,它会返回用户想要的书名/作者姓名。
谢谢
最佳答案
使用 contains()
Xpath 函数。
//book[contains(content, '%s')]/bookname
取决于您输入的 XML 的结构
关于java - 使用 XPath 查询搜索字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676256/