How to extract RDF triples from XML file using an existing ontology?

孤街醉人 提交于 2019-12-22 06:49:03

问题


I am trying to extract RDF triples from XML files by using an existing ontology. I am using Java, and can use XPath to extract data from XML and Jena to read and write RDF documents and ontologies. How can I extract the relevant triples from the XML according to the existing ontology?


回答1:


Forget about XPath to extract triples, it way easier and less problematic with Jena.

You can use the interface SimpleSelector together with model.listStatements from Jena.

In this example I am using SimpleSelector to find all the triples with a single property but you can implement the any search you need by customizing the method selects.

FileManager fManager = FileManager.get();
Model model = fManager.loadModel("some_file.rdf");

Property someRelevantProperty = 
    model. createProperty("http://your.data.org/ontology/",
                          "someRelevantProperty");

SimpleSelector selector = new SimpleSelector(null, null, (RDFNode)null) {
    public boolean selects(Statement s)
        { return s.getPredicate().equals(someRelevantProperty);}
}

StmtIterator iter = model.listStatements(selector);
while(it.hasNext()) {
   Statement stmt = iter.nextStatement();
   System.out.print(stmt.getSubject().toString());
   System.out.print(stmt.getPredicate().toString());
   System.out.println(stmt.getObject().toString());
}

You'll find more information here.

If you describe a bit more the ontology you are using and the type of search you need we might be able to help more.



来源:https://stackoverflow.com/questions/5669422/how-to-extract-rdf-triples-from-xml-file-using-an-existing-ontology

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!