Iterating Statements of RDF file in Java

Each and every arc in RDF Model represents a statement and each statement have three parts in it.

Iterating Statements of RDF file in Java

Iterating Statements of RDF file in Java

     

Each and every arc in RDF Model represents a statement and each statement have three parts in it as follows :

  • Subject 
  • Predicate
  • Object

Subject is the resource from where the arc leaves or we can say that it is a source, Predicate is the label of the leave and Object is the resource to which arc points out or we can say it as sink. It is sometimes called triple (Subject,Predicate,Object). To iterate RDF model statements we need StmtIterator. With Statement object we can access all properties of that Statement such as subject, predicate, object (i.e triple). Full source code of RDFIterator.java is as follows :-

RDFIterator.java

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

public class RDFIterator 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));
  StmtIterator iter = model.listStatements();
  while (iter.hasNext()) {
  Statement stmt  = iter.nextStatement()
  Resource  subject = stmt.getSubject()
  Property  predicate = stmt.getPredicate();  
  RDFNode object  = stmt.getObject();  
  System.out.print(subject.toString());
  System.out.print(" " + predicate.toString() " ");
  if (object instanceof Resource) {
  System.out.print(object.toString());
  else {
  System.out.print("\"" + object.toString() "\"");
  }
  System.out.println(".");
  }
  }
}

To run this program you would have to follow these steps:

  • Create and save RDFIterator.java
  • Compile RDFIterator.java
  • Run RDFIterator class file.

Output:

After executing RDFIterator you will get the following output.


Download Source Code