Question

How can I generate the following RDF/XML using Jena?

<rdfs:Class rdf:about="http://example.com/A#B">
    <rdfs:subClassOf>
            <rdfs:Class rdf:about="http://example.com/A" />
     </rdfs:subClassOf>
        <rdf:Property rdf:about="http://example.com/C">
            <rdfs:range rdf:resource="http://example.com/A" />
        </rdf:Property>
</rdfs:Class>   
Was it helpful?

Solution

There are many Jena tutorials on the web. However, what you are asking for is pretty straightforward. Here's one solution:

package example;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;

class RdfXmlExample {
    public static void main( String[] args ) {
        new RdfXmlExample().run();
    }

    public void run() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
        String NS = "http://example.com/test#";

        OntClass a = m.createClass( NS + "A" );
        OntClass b = m.createClass( NS + "B" );

        a.addSubClass( b );

        OntProperty c = m.createOntProperty( NS + "c" );
        c.addRange( a );

        m.write( System.out, "RDF/XML-ABBREV" );
    }
}

which produces:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdfs:Class rdf:about="http://example.com/test#B">
    <rdfs:subClassOf>
      <rdfs:Class rdf:about="http://example.com/test#A"/>
    </rdfs:subClassOf>
  </rdfs:Class>
  <rdf:Property rdf:about="http://example.com/test#c">
    <rdfs:range rdf:resource="http://example.com/test#A"/>
  </rdf:Property>
</rdf:RDF>

OTHER TIPS

@Ian Dickinson's answer is spot on. If you wish to write this output to a file you can use this line instead

 m.write( new FileWriter("some-file.owl"), "RDF/XML-ABBREV" );

You can then view this file either through protege or on WebVowl.

Code + explanation of

  • Jena RDF API tutorial
  • SPARQL tutorial
  • Ontology API overview

http://incubator.apache.org/jena/getting_started/index.html

decent place to start with jena.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top