You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by vijayalakshmi <vi...@gmail.com> on 2012/03/24 18:28:26 UTC

reading value from an RDF file into java

The following is my RDF file structure.

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
  <rdf:Description rdf:about="http://www.wordpress.com/blogs/ckj">
    <rdf:subject>Data structures</rdf:subject>
    <rdf:object>vijayalakshmi</rdf:object>
    <rdf:value>kjhjk</rdf:value>
  </rdf:Description>
</rdf:RDF>


I want to write a piece of java code that can
       -->  read the value of <rdf:subject> <rdf:object> into a model
inside a java program.

How should I do this??

I am a newbie. So, please help!!

Re: Fwd: reading value from an RDF file into java

Posted by Andy Seaborne <an...@apache.org>.
This is a Java issue, not to do with Jena itself:

 > Caused by: java.lang.ClassNotFoundException:
 > com.hp.hpl.jena.rdf.model.ModelFact
 > ory

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassNotFoundException.html


 > java RdRDF

You have not set the classpath by the look of it.

Either set the classpath or run inside Eclipse or other IDE which will 
do it for you when you set the project up correctly.

	Andy


On 25/03/12 15:46, vijayalakshmi wrote:
> The problem I initially told was a part of the entire idea.
>
> Here is what I require.
>
> I want to write a java program that should look through all the RDF files
> in a given folder. The java program is given a string as input.
> The program has to look for the value of<RDF.subject>  element in each of
> the RDF file to see if the value matches with the given input string. If
> there is a match, then the value of the<RDF.object>  should be obtained and
> stored in a string array.
>
> The following is my java code.
>
> import java.io.*;
> import java.io.File;
>
> import java.io.FileOutputStream;
> import java.io.IOException;
>
> import com.hp.hp> Caused by: java.lang.ClassNotFoundException:
> com.hp.hpl.jena.rdf.model.ModelFact
> oryl.jena.rdf.model.*;
> import com.hp.hpl.jena.vocabulary.*;
> import com.hp.hpl.jena.util.FileManager;
>
> public class RdRDF extends Object
> {
> public static void main(String args[])
> {
> try
> {
> // create an empty model
> Model model = ModelFactory.createDefaultModel();
>   //look at all the files in the folder
> String path = "C:\\Program Files\\Apache Software Foundation\\Tomcat
> 6.0\\webapps\\MyFiles\\WEB-INF\\classes\\";
> String files;
> File folder = new File(path);
> File[] listOfFiles = folder.listFiles();
>
> for (int i = 0; i<  listOfFiles.length; i++)
> {
> if (listOfFiles[i].isFile())
> {
> files = listOfFiles[i].getName();
> if (files.endsWith(".rdf"))
> {
>   model.read(files, null);
>   //read the domain
> System.out.println(files + "\t");
> Resource vcard = model.getResource("http://www.wordpress.com/blogs/" +
> files);
> String domain = vcard.getProperty(RDF.subject).getString();
>   System.out.println(domain + "\n");
> }
> }
> }
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
> }
> }
>
>
> When I run this code,
>
> I get the following error.
>
> C:\Program Files\Apache Software Foundation\Tomcat
> 6.0\webapps\MyFiles\WEB-INF\c
> lasses>java RdRDF
> Exception in thread "main" java.lang.NoClassDefFoundError:
> com/hp/hpl/jena/rdf/m
> odel/ModelFactory
>          at RdRDF.main(RdRDF.java:18)
> Caused by: java.lang.ClassNotFoundException:
> com.hp.hpl.jena.rdf.model.ModelFact
> ory
>          at java.net.URLClassLoader$1.run(Unknown Source)
>          at java.security.AccessController.doPrivileged(Native Method)
>          at java.net.URLClassLoader.findClass(Unknown Source)
>          at java.lang.ClassLoader.loadClass(Unknown Source)
>          at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
>          at java.lang.ClassLoader.loadClass(Unknown Source)
>          ... 1 more
>
> C:\Program Files\Apache Software Foundation\Tomcat
> 6.0\webapps\MyFiles\WEB-INF\c
> lasses>
>
>
> Please tell me what mistake I have done.
>


Re: Fwd: reading value from an RDF file into java

Posted by vijayalakshmi <vi...@gmail.com>.
The problem I initially told was a part of the entire idea.

Here is what I require.

I want to write a java program that should look through all the RDF files
in a given folder. The java program is given a string as input.
The program has to look for the value of <RDF.subject> element in each of
the RDF file to see if the value matches with the given input string. If
there is a match, then the value of the <RDF.object> should be obtained and
stored in a string array.

The following is my java code.

import java.io.*;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
import com.hp.hpl.jena.util.FileManager;

public class RdRDF extends Object
{
public static void main(String args[])
{
try
{
// create an empty model
Model model = ModelFactory.createDefaultModel();
 //look at all the files in the folder
String path = "C:\\Program Files\\Apache Software Foundation\\Tomcat
6.0\\webapps\\MyFiles\\WEB-INF\\classes\\";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".rdf"))
{
 model.read(files, null);
 //read the domain
System.out.println(files + "\t");
Resource vcard = model.getResource("http://www.wordpress.com/blogs/" +
files);
String domain = vcard.getProperty(RDF.subject).getString();
 System.out.println(domain + "\n");
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


When I run this code,

I get the following error.

C:\Program Files\Apache Software Foundation\Tomcat
6.0\webapps\MyFiles\WEB-INF\c
lasses>java RdRDF
Exception in thread "main" java.lang.NoClassDefFoundError:
com/hp/hpl/jena/rdf/m
odel/ModelFactory
        at RdRDF.main(RdRDF.java:18)
Caused by: java.lang.ClassNotFoundException:
com.hp.hpl.jena.rdf.model.ModelFact
ory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

C:\Program Files\Apache Software Foundation\Tomcat
6.0\webapps\MyFiles\WEB-INF\c
lasses>


Please tell me what mistake I have done.

Re: Fwd: reading value from an RDF file into java

Posted by Dave Reynolds <da...@gmail.com>.
On 25/03/12 14:18, vijayalakshmi wrote:
> I did. I also read a tutorial given in the following link.
>
> http://jena.sourceforge.net/tutorial/RDF_API/#ch-Jena RDF Packages
>
> But then, I am not able to read the values and store it in a string in a
> servlet.
>
> This is what I have done so far.
>
> //read the file into a model
> model.read(files, null);
>
> //read the domain
> Resource vcard = model.getResource("http://www.wordpress.com/blogs/" +
> files);

The URI must match the resource you are trying to access. Unless "files" 
is set to "jhbh" then this won't match your data sample below.

> String domain = vcard.getProperty(RDF.subject).getString();
>
> If the input file is the following,
>
> <rdf:RDF
>      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
>    <rdf:Description rdf:about="http://www.wordpress.com/blogs/jhbh">
>      <rdf:subject>Data structures</rdf:subject>

[Aside: That's a odd way to use rdf:subject, you may mean dcterms:subject.]

>      <rdf:object></rdf:object>
>      <rdf:value>hbl</rdf:value>
>    </rdf:Description>
> </rdf:RDF>
>
> I want the String domain to be "Data Structures".

If I put that in a file "temp.rdf" and run:

     Model model = FileManager.get().loadModel("temp.rdf");
     Resource vcard = model.getResource(
            "http://www.wordpress.com/blogs/jhbh");
     String domain = vcard.getProperty(RDF.subject).getString();
     System.out.println("Domain = " + domain);

I get:

     Domain = Data structures

Dave


Re: Fwd: reading value from an RDF file into java

Posted by vijayalakshmi <vi...@gmail.com>.
I did. I also read a tutorial given in the following link.

http://jena.sourceforge.net/tutorial/RDF_API/#ch-Jena RDF Packages

But then, I am not able to read the values and store it in a string in a
servlet.

This is what I have done so far.

//read the file into a model
model.read(files, null);

//read the domain
Resource vcard = model.getResource("http://www.wordpress.com/blogs/" +
files);
String domain = vcard.getProperty(RDF.subject).getString();

If the input file is the following,

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
  <rdf:Description rdf:about="http://www.wordpress.com/blogs/jhbh">
    <rdf:subject>Data structures</rdf:subject>
    <rdf:object></rdf:object>
    <rdf:value>hbl</rdf:value>
  </rdf:Description>
</rdf:RDF>

I want the String domain to be "Data Structures".

How should I do that??

I want to open an RDF file and read the value of an element in that file.
It sounds simple. But i am not able to get it.

Re: Fwd: reading value from an RDF file into java

Posted by Paolo Castagna <ca...@googlemail.com>.
Hi

Andy has already pointed you at the Jena tutorial, you should really read that.

Here are a few additional examples you might find useful:
https://github.com/castagna/jena-examples/blob/master/src/main/java/org/apache/jena/examples/ExampleAPI_03.java
https://github.com/castagna/jena-examples/blob/master/src/main/java/org/apache/jena/examples/ExampleIO_01.java

Have a look at the Javadoc as well:
http://incubator.apache.org/jena/documentation/javadoc/jena/
http://incubator.apache.org/jena/documentation/javadoc/jena/com/hp/hpl/jena/rdf/model/Model.html

Paolo

vijayalakshmi wrote:
> The following is my RDF file structure.
> 
> <rdf:RDF
>     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
>   <rdf:Description rdf:about="http://www.wordpress.com/blogs/ckj">
>     <rdf:subject>Data structures</rdf:subject>
>     <rdf:object>vijayalakshmi</rdf:object>
>     <rdf:value>kjhjk</rdf:value>
>   </rdf:Description>
> </rdf:RDF>
> 
> 
> I want to write a piece of java code that can
>        -->  read the value of <rdf:subject> <rdf:object> into a model
> inside a java program.
> 
> How should I do this??
> 
> I am a newbie. So, please help!!
> 

Fwd: reading value from an RDF file into java

Posted by vijayalakshmi <vi...@gmail.com>.
The following is my RDF file structure.

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
  <rdf:Description rdf:about="http://www.wordpress.com/blogs/ckj">
    <rdf:subject>Data structures</rdf:subject>
    <rdf:object>vijayalakshmi</rdf:object>
    <rdf:value>kjhjk</rdf:value>
  </rdf:Description>
</rdf:RDF>


I want to write a piece of java code that can
       -->  read the value of <rdf:subject> <rdf:object> into a model
inside a java program.

How should I do this??

I am a newbie. So, please help!!

Re: reading value from an RDF file into java

Posted by Andy Seaborne <an...@apache.org>.
On 24/03/12 17:28, vijayalakshmi wrote:
> The following is my RDF file structure.
>
> <rdf:RDF
>      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
>    <rdf:Description rdf:about="http://www.wordpress.com/blogs/ckj">
>      <rdf:subject>Data structures</rdf:subject>
>      <rdf:object>vijayalakshmi</rdf:object>
>      <rdf:value>kjhjk</rdf:value>
>    </rdf:Description>
> </rdf:RDF>
>
>
> I want to write a piece of java code that can
>         -->   read the value of<rdf:subject>  <rdf:object>  into a model
> inside a java program.
>
> How should I do this??
>
> I am a newbie. So, please help!!

See the RDF tutorial
http://incubator.apache.org/jena/tutorials/

and start by reading in the RDF and output it as N-triples or Turtle to 
see the structure as triples.

	Andy