Home Tutorials Rdf Writing RDF file in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Writing RDF file in Java

Advertisement
Jena have provided the methods which can read and write RDF files. RDF stands for Resource Description Framework that is used to represents the maximum information of resources available in the World Wide Web.

Writing RDF file in Java

     

Jena have provided the methods which can read and write RDF files. RDF stands for Resource Description Framework that is used to represents the maximum information of resources available in the World Wide Web. 

This example includes everything that you need to create an application that writes RDF or Resource Description Framework file in Java. The example illustrates you how to write a RDF/XML files with separate source code and examples. With the help of the given example, we have tried to simplify this concept to you to learn it quickly and concentrate on the actual part that RDF.

Guideline and example code to develop RDF file in Java

"createDefaultModel()" method of ModelFactory returns an object of Model interface.

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));

Above lines of code creates an object of Model interface and creates resource object and then we can add different properties to this node. To write this model we have used write(java.io.OutputStream out) method of Model interface.

RDFWriter.java

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

public class RDFWriter extends Object {
  public static void main (String args[]) {
  String personURI  = "http://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));
  try{
  FileOutputStream fout=new FileOutputStream(
 
"C:\\Java\\jdk1.6.0_03\\bin\\amitKumar.xml");
  model.write(fout);
  }catch(IOException e){
  System.out.println("Exception caught"+e.getMessage());
  }
  }
}

To run this example you have to follow these following steps:

  • Create and Save RDFWriter.java
  • Compile RDFWriter.java
  • Run RDFWriter.class file and you will get amitkumar.xml file in your specified folder.

Output:  

You will have "amitkumar.xml" in your bin folder and content of this xml file will look this:

<rdf:RDF
  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://amitKumar">
   <vcard:N rdf:nodeID="A0"/>
   <vcard:FN>Amit Kumar</vcard:FN>
</rdf:Description>
<rdf:Description rdf:nodeID="A0">
   <vcard:Family>Kumar</vcard:Family>
   <vcard:Given>Amit</vcard:Given>
</rdf:Description>
</rdf:RDF>


Download Source Code

     

Advertisements

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

Ask Questions?    Discuss: Writing RDF file in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Reza
May 4, 2012
Use Ontology

This is ok but how can we use another ontology like owl:sameAs while writing RDF file in jena? your example contains only VCARD!
DMCA.com