I've changed the namespace a bit, just so that this code ends up pointing back to this post, but at any rate, I get this output:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdf:Property rdf:about="http://stackoverflow.com/q/20194409/1281433/PropertyName">
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
<rdfs:domain rdf:resource="http://stackoverflow.com/q/20194409/1281433/ClassName"/>
</rdf:Property>
</rdf:RDF>
from this code:
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.vocabulary.RDFS;
public class JenaPropertyExample {
public static void main(String[] args) {
final String NS = "http://stackoverflow.com/q/20194409/1281433/";
final Model model = ModelFactory.createDefaultModel();
final Property p = model.createResource( NS+"PropertyName", RDF.Property ).as( Property.class );
p.addProperty( RDFS.domain, model.createResource( NS+"ClassName" ));
p.addProperty( RDFS.range, RDFS.Literal );
model.write( System.out, "RDF/XML-ABBREV" );
}
}
In general, just because the output is legal RDF doesn't mean that you're using the properties and classes in the way that's expected. Jena's plain Model
interface can't help you too much with that, since you could still use the properties incorrectly, but at least, if you're using Jena's predefined vocabulary classes, you'll get the IRIs right. If you can use an OntModel, you can get a slightly nicer layer of abstraction, though. E.g., the following method produces the same RDF/XML output, but lets you use methods like createOntProperty
and get the p rdf:type rdf:Property
triple for free, and methods like addRange
and addDomain
:
public static void main2( String[] args ) {
final String NS = "http://stackoverflow.com/q/20194409/1281433/";
final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
OntProperty p = model.createOntProperty( NS+"PropertyName" );
p.addDomain( model.getOntClass( NS+"ClassName" ));
p.addRange( RDFS.Literal );
model.write( System.out, "RDF/XML-ABBREV" );
}