gpt4 book ai didi

java - Java 中包含命名空间的 xpath 问题(Dom 解析器)

转载 作者:行者123 更新时间:2023-12-01 23:21:17 25 4
gpt4 key购买 nike

我正在尝试解析 rdfs xml 文件,以便查找 rdfs 文件中的所有类。

xpath:“/rdf:RDF/rdfs:Class”正在我的 XML 编辑器中工作。当我在 Java 程序中插入 xpath 时(我已经实现了 dom 解析器),我得到 0 个类。以下示例运行但输出 0 个类!

我愿意:

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;

public class Main {
public static void main(String args[]) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException{

FindClasses FSB = new FindClasses();
FSB.FindAllClasses("C:\\Workspace\\file.xml"); //rdfs file
}
}

FindClasses 类如下:

import java.io.IOException;
import java.util.Collection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FindClasses {

public void FindAllClasses(String fileName) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {


DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(fileName);

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression classes_expr = xpath.compile("/rdf:RDF/rdfs:Class");

Object result = classes_expr.evaluate(doc, XPathConstants.NODESET);
NodeList classes = (NodeList) result;

System.out.println("I found : " + classes.getLength() + " classes " );

}
}

rdfs 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xml:lang="en" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdfs:Class rdf:about="Class1">
</rdfs:Class>
<rdfs:Class rdf:about="Class2">
</rdfs:Class>
</rdf:RDF>

我真的不明白为什么 xpath 在该示例中返回 0 个节点。这很奇怪,因为我也实现了其他 dom 解析器,并且它们工作正常。有人可以帮助我吗?

谢谢

最佳答案

我访问了以下链接并解决了我的问题:<强> Issues with xpath in java

问题是 xpath 包含两个命名空间(rdf、rdfs),例如“/rdf:RDF/rdfs:Class”。如果 xpath 不包含任何 namespace ,例如/RDF/Class ,不会有问题。

所以行之后:

xpath = XPathFactory.newInstance().newXPath();

和行前:

XPathExpression classes_expr = xpath.compile("/rdf:RDF/rdfs:Class");    

添加了以下内容:

xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
switch (prefix) {
case "rdf": return "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
case "rdfs" : return "http://www.w3.org/2000/01/rdf-schema#";
}
return prefix;

}
public String getPrefix(String namespace) {
if (namespace.equals("rdf")) return "rdf";
else if (namespace.equals("rdfs")) return "rdfs";
else return null;
}
@Override
public Iterator getPrefixes(String arg0) {
// TODO Auto-generated method stub
return null;
}
});

关于java - Java 中包含命名空间的 xpath 问题(Dom 解析器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20581938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com