java - 使用 XPath 查询搜索字符串的一部分

标签 java xml xpath

我正在使用以下 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/

相关文章:

java - 安卓错误: cannot find symbol variable menu

java - Selenium Webdriver - 从动态表访问数据

java - 如何访问 SOAP 消息元素?

java - jar 配置及其内容

ruby-on-rails - 在 XML 响应中返回错误消息

python - Python 将 UTF 字符从 XML 插入 MySQL 时出现问题

xpath - 使用 Hamcrest 进行 Spring MVC 测试 : How test size and value for a Generic Collection

java - 如何在tomcat中设置Oracle时区区域

java - 是否有像 Eclipse : Switching to debugging/testing layout 这样的 Netbeans 配置文件 View

java - ScrollView 无法滚动