This is a tutorial introduction to both W3C's Resource Description Framework (RDF) and Jena, a Java API for RDF. It is written for the beginners who wants to have a basic idea of how to RDF graph is generate through JAVA.
An implementation of the Jena API can be downloaded from http://jena.sourceforge.net.
Prerequisite for user to have basic knowledge of
- RDF Framework
- xml
To run the example you have to follow these steps:
- Create and save the code as GenerateRDF.java
- Add "jena.jar, icu4j_3_4.jar, iri.jar, commons-logging-1.1.1.jar, xercesImpl.jar" to your classpath.
- Compile GenerateRDF file and
- Run GenerateRDF class file
More over if you are using Eclipse. you just need to add these jar file in the project>properties>JAVA BUILD PATH
To create RDF file we do need to import following packages:
- "com.hp.hpl.jena.rdf.model" : for creating and manipulating model resources.
- "com.hp.hpl.jena.vocabulary": for creating VCARD vocabulary.
package com.generategraph;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.vocabulary.VCARD;
public class GenerateRDF {
public static void main(String[] args) {
String personURI="http://localhost:8080/Gautam";
String firstName="Gautam";
String lastName="Choraria";
String fullName="Gautam Choraria";
//create an empty model
Model model =ModelFactory.createDefaultModel();
Resource node=model.createResource(personURI);
node.addProperty(VCARD.FN,fullName);
Resource blankNode=model.createResource();
node.addProperty(VCARD.N, blankNode);
blankNode.addProperty(VCARD.Given, firstName);
blankNode.addProperty(VCARD.Family, lastName);
//display rdf graph in xml format on to the screen
model.write(System.out);
}
}
You will get following output
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" >
<rdf:Description rdf:about="http://localhost:8080/Gautam">
<vcard:FN>Gautam Choraria</vcard:FN>
<vcard:N rdf:nodeID="A0"/>
</rdf:Description>
<rdf:Description rdf:nodeID="A0">
<vcard:Given>Gautam</vcard:Given>
<vcard:Family>Choraria</vcard:Family>
</rdf:Description>
</rdf:RDF>