More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Reading RDF file in Java

                         

As you know that The Resource Description Framework is used to represents the information, so the particular RDF Example is written to explain you how to read the RDF files in Java. In the next example you will be learning about writing a RDF file in Java. But first lets see how to read it..? 

To read and show the data of RDF/XML file on command prompt, just follow the given example. It is going to read a RDF/XML file and then its content will be shown on command prompt.

To read RDF/XML file we do have need an object of Model interface. It takes two arguments :

  • first is java.io.Reader class object and
  • second is String class object.

File which is to be read is being get by FileInputStream class constructor. Then read(in,"") method reads all file.

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

public class FirstRDFReader extends Object {
      public static void main (String args[]) {
    String inputFile="amit.xml";
      Model model = ModelFactory.createDefaultModel();
     try{
       InputStream in =new  FileInputStream(inputFile);
    if (in == null) {      
      System.out.println("File not found");
         }          
    model.read(in," ");
    model.write(System.out);
     }catch(Exception e){}
  }
}

Our XML file will look like this.

amit.xml

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

To run this example:

  • Create and save FirstRDFReader.java
  • Compile FirstRDFReader.java
  • Execute FirstRDFReader.class file and you will get following output on your command prompt.

Output:

Download Source Code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Join and Excel yourself with our Online instructor led training sessions
Training Courses
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.