Java中包含名称空间的xpath问题(Dom解析器)
我试图解析一个rdfs xml文件,以便找到一个rdfs文件中的所有类 xpath:“/rdf:rdf/rdfs:Class” 正在我的XML编辑器中工作。 当我在Java程序中插入xpath时(我已经实现了dom解析器),我得到了0个类。 下面的示例运行,但它输出0个类 我有: FindClass类如下所示:Java中包含名称空间的xpath问题(Dom解析器),java,parsing,dom,rdfs,Java,Parsing,Dom,Rdfs,我试图解析一个rdfs xml文件,以便找到一个rdfs文件中的所有类 xpath:“/rdf:rdf/rdfs:Class” 正在我的XML编辑器中工作。 当我在Java程序中插入xpath时(我已经实现了dom解析器),我得到了0个类。 下面的示例运行,但它输出0个类 我有: FindClass类如下所示: import java.io.IOException; import java.util.Collection; import javax.xml.parsers.DocumentBui
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解析器,它们工作得很好。
有人能帮我吗
谢谢我访问了以下链接并解决了我的问题: 问题是xpath包含两个名称空间(rdf,rdf),如“/rdf:rdf/rdfs:Class”。 如果xpath不包含任何名称空间,例如/RDF/Class,那么就不会有问题 因此,在行之后:
xpath = XPathFactory.newInstance().newXPath();
XPathExpression classes_expr = xpath.compile("/rdf:RDF/rdfs: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;
}
});
我访问了以下链接并解决了我的问题: 问题是xpath包含两个名称空间(rdf,rdf),如“/rdf:rdf/rdfs:Class”。 如果xpath不包含任何名称空间,例如/RDF/Class,那么就不会有问题 因此,在行之后:
xpath = XPathFactory.newInstance().newXPath();
XPathExpression classes_expr = xpath.compile("/rdf:RDF/rdfs: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;
}
});
我访问了以下链接并解决了我的问题: 问题是xpath包含两个名称空间(rdf,rdf),如“/rdf:rdf/rdfs:Class”。 如果xpath不包含任何名称空间,例如/RDF/Class,那么就不会有问题 因此,在行之后:
xpath = XPathFactory.newInstance().newXPath();
XPathExpression classes_expr = xpath.compile("/rdf:RDF/rdfs: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;
}
});
我访问了以下链接并解决了我的问题: 问题是xpath包含两个名称空间(rdf,rdf),如“/rdf:rdf/rdfs:Class”。 如果xpath不包含任何名称空间,例如/RDF/Class,那么就不会有问题 因此,在行之后:
xpath = XPathFactory.newInstance().newXPath();
XPathExpression classes_expr = xpath.compile("/rdf:RDF/rdfs: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;
}
});
如果您不想自己麻烦地实现
namespacecoxt
,如果您不想自己麻烦地实现namespacecoxt
,那么有一个有用的通用实现,如果您不想自己麻烦地实现名称空间上下文
,那么有一个有用的通用实现;如果您不想自己麻烦地实现名称空间上下文
,那么有一个有用的通用实现