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.

Writing RDF file in Java

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