Java XPath 扫描文件寻找单词
Java XPath scan file looking for a word
我正在构建一个应用程序,该应用程序将从用户那里获取一个词,然后使用 XPath 扫描文件,返回 true 或 false,具体取决于是否在该文件中找到该词。
我在 class 之后构建了实现 XPath 的版本,但我要么误解了它应该如何工作,要么我的代码有问题。谁能给我解释一下如何使用 Xpath 进行全文件搜索?
public XPath() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
FileInputStream fileIS = new FileInputStream("text.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fileIS);
XPathFactory xPathfactory = XPathFactory.newInstance();
javax.xml.xpath.XPath xPath = xPathfactory.newXPath();
XPathExpression expr = xPath.compile("//text()[contains(.,'java')]");
System.out.println(expr.evaluate(xmlDocument, XPathConstants.NODESET));
}
还有我目前正在测试的 xml 文件。
<?xml version="1.0"?>
<Tutorials>
<Tutorial tutId="01" type="java">
<title>Guava</title>
<description>Introduction to Guava</description>
<date>04/04/2016</date>
<author>GuavaAuthor</author>
</Tutorial>
<Tutorial tutId="02" type="java">
<title>XML</title>
<description>Introduction to XPath</description>
<date>04/05/2016</date>
<author>XMLAuthor</author>
</Tutorial>
</Tutorials>
找到解决方案,我没有正确显示找到的条目,正如有人在评论中指出的那样 'java' 在参数中,我只想扫描文本字段,因此在添加后永远找不到遵循代码并更改我的应用程序将查找的词,应用程序有效
Object result = expr.evaluate(xmlDocument, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
您的 XPath 正在搜索 text()
节点,但单词 java
出现在 @type
属性(不是 text()
节点)中。
如果您想在 text()
和 @*
中搜索单词,那么您可以使用联合 |
运算符并检查包含该单词的 either/both:
//text()[contains(. ,'java')] | //@*[contains(., 'java')]
但您可能还想扫描 comment()
和 processing-instruction()
,因此可以在 node()
上进行一般匹配,然后在谓词测试中:
//node()[contains(. ,'java')] | //@*[contains(., 'java')]
对于 XPath 2.0 或更高版本,您可以使用:
//node()[(.|@*)[contains(., 'java')]]
我正在构建一个应用程序,该应用程序将从用户那里获取一个词,然后使用 XPath 扫描文件,返回 true 或 false,具体取决于是否在该文件中找到该词。
我在 class 之后构建了实现 XPath 的版本,但我要么误解了它应该如何工作,要么我的代码有问题。谁能给我解释一下如何使用 Xpath 进行全文件搜索?
public XPath() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
FileInputStream fileIS = new FileInputStream("text.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fileIS);
XPathFactory xPathfactory = XPathFactory.newInstance();
javax.xml.xpath.XPath xPath = xPathfactory.newXPath();
XPathExpression expr = xPath.compile("//text()[contains(.,'java')]");
System.out.println(expr.evaluate(xmlDocument, XPathConstants.NODESET));
}
还有我目前正在测试的 xml 文件。
<?xml version="1.0"?>
<Tutorials>
<Tutorial tutId="01" type="java">
<title>Guava</title>
<description>Introduction to Guava</description>
<date>04/04/2016</date>
<author>GuavaAuthor</author>
</Tutorial>
<Tutorial tutId="02" type="java">
<title>XML</title>
<description>Introduction to XPath</description>
<date>04/05/2016</date>
<author>XMLAuthor</author>
</Tutorial>
</Tutorials>
找到解决方案,我没有正确显示找到的条目,正如有人在评论中指出的那样 'java' 在参数中,我只想扫描文本字段,因此在添加后永远找不到遵循代码并更改我的应用程序将查找的词,应用程序有效
Object result = expr.evaluate(xmlDocument, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
您的 XPath 正在搜索 text()
节点,但单词 java
出现在 @type
属性(不是 text()
节点)中。
如果您想在 text()
和 @*
中搜索单词,那么您可以使用联合 |
运算符并检查包含该单词的 either/both:
//text()[contains(. ,'java')] | //@*[contains(., 'java')]
但您可能还想扫描 comment()
和 processing-instruction()
,因此可以在 node()
上进行一般匹配,然后在谓词测试中:
//node()[contains(. ,'java')] | //@*[contains(., 'java')]
对于 XPath 2.0 或更高版本,您可以使用:
//node()[(.|@*)[contains(., 'java')]]