网上有很多 Jena 教程。但是,您的要求非常简单。这是一个解决方案:
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" );
}
}
产生:
<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>