简体   繁体   中英

Issues with xpath that contained namespaces in Java (Dom parser)

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

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!

I do:

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
     }
}

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 " );

        }
    }

The rdfs file is:

<?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>  

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?

Thanks

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

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

So after the line:

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

and before the line:

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

I added the following:

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;
    }
    }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM