xpath包含Java中的名称空间的问题(Dom解析器) [英] Issues with xpath that contained namespaces in Java (Dom parser)

查看:106
本文介绍了xpath包含Java中的名称空间的问题(Dom解析器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to parse an rdfs xml file in order to find all the Classes in an rdfs file.

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

The xpath: "/rdf:RDF/rdfs:Class" is working in my XML editor. When i insert the xpath in my Java program (i have implemented a dom parser), i get 0 Classes. The following example runs but it outputs 0 classes!

我愿意

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类如下:

The class FindClasses is as follows:

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

I don't really understand why the xpath returns 0 nodes in that example. It's weird, cause i have implemented other dom parsers as well and they were working fine. Can somebody help me?

谢谢

推荐答案

我访问了以下链接,并解决了我的问题: Java中的xpath问题

I visited the following link and i solved my problem: Issues with xpath in java

问题是xpath包含两个名称空间(rdf,rdfs),例如"/rdf:RDF/rdfs:Class". 如果xpath不包含任何名称空间,例如/RDF/Class,就不会有问题了.

The problem was that the xpath contained two namespaces (rdf,rdfs) like "/rdf:RDF/rdfs:Class". If the xpath didn't contain any namespace e.g. /RDF/Class , there was not going to be an issue.

因此,在该行的之后:

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包含Java中的名称空间的问题(Dom解析器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆