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

时间:2013-12-14 10:05:30

标签: java parsing dom rdfs

我正在尝试解析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解析器,并且它们工作正常。 有人可以帮帮我吗?

由于

1 个答案:

答案 0 :(得分:3)

我访问了以下链接,我解决了我的问题: 的 Issues with xpath in java

问题是xpath包含两个名称空间(rdf,rdfs),如“/ rdf:RDF / rdfs:Class”。 如果xpath不包含任何名称空间,例如/ 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;
    }
    });