Search code examples
javardfjenaowl

Jena API - How can I handle OWL files?


I have the below code to list individuals of an ontology:

public static void main(String[] args) {
    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    String fileName = "C:/Users/Ikno/Desktop/workspace/Jena/ontrdf.rdf";

    try {
        InputStream inputStream = new FileInputStream(fileName);
        model.read(inputStream, "RDF/XML");
        //model.read(inputStream, "OWL/XML");
        inputStream.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    ExtendedIterator<Individual> itI = model.listIndividuals();

    while (itI.hasNext()) {
        Individual i = itI.next();
        System.out.println(i.getLocalName());
    }
}

The code is OK, and it returns all the individual local names. The problem is that I created the OWL ontology in Protégé and it is a wasteful step to convert it to RDF, just to be operated by Jena. If I uncomment model.read(inputStream, "OWL/XML");, it gives me the following error:

class org.apache.jena.riot.RiotException
[line: 266, col: 31] {E201} Multiple children of property element

Can't Jena support this kind of operation with OWL format?


Solution

  • According to Jena Documentation here If you look at the read method, it is clear that Jena does not support OWL/XML. Predefined values for lang (Second Arg to read method) are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML". So in your case I would save the ontology as RDF/XML and then read it with Jena.