Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Tutorials Rdf Generate RDF file in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Generate RDF file in Java

Advertisement
In this example we are going to generate our first RDF( Resource Description File).

Generate RDF file in Java

     

In this example we are going to generate our first RDF( Resource Description File). This example generates a  RDF file with the use of "Jena". Jena is a java API to create and manipulate RDF graphs.

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.

To create model we have used createDefaultModel() of ModelFactory, which in turn returns an object of Model interface. 

Model model = ModelFactory.createDefaultModel();

To create resource we have used createResource() method of Model interface. Now we can add properties in this model resource by using the "addProperty()" method.

GenerateRDF.java

import java.io.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;

public class GenerateRDF extends Object {
  public static void main (String args[]) {
  String personURI  = "http://localhost/amitkumar";
  String givenName  = "Amit";
  String familyName = "Kumar";
  String fullName = givenName+familyName;

  Model model = ModelFactory.createDefaultModel();

  Resource node = model.createResource(personURI)
 .addProperty(VCARD.FN, fullName)
 .addProperty(VCARD.N,
  model.createResource()
 .addProperty(VCARD.Given, givenName)
 .addProperty(VCARD.Family, familyName));
  model.write(System.out);
  }
}

To run the example you have to follow these steps:

  1. Create and save the code as GenerateRDF.java
  2. Add "jena.jar, icu4j_3_4.jar, iri.jar, commons-logging-1.1.1.jar, xercesImpl.jar" to your classpath.
  3. Compile GenerateRDF file and
  4. Run GenerateRDF class file

You will get following output on your command prompt.


Download Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 27, 2008

Related Tutorials

Discuss: Generate RDF file in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
praveenkumar arepalli
February 27, 2013
RDF generation

it was my final year project, highly useful for me, thanks roseIndia
lilya
April 6, 2012
help

hi,please i would know what mean the String personURI = "http://localhost/amitkumar"; String givenName = "Amit"; String familyName = "Kumar"; is it the uri of my ontology or the uri of the generated file? and givenName="amit" it's a choise or what please i need your help thanks
Parvathy
October 10, 2012
Problem in GenerateRDF file

Am getting errors, when I run this program in command prompt and also in eclipse. I've installed all jar files mentioned to class path. Please can you help me out with this problem.
vinodh
June 17, 2014
splendid

help needed
DMCA.com