1

I have this xml file, how can I create RDF triple from it using xpath and ModelFactory in java?

<xml>
<person>
<name>Joe</name>
<website url="www.example1.com">contact1</website >
<vote>20</vote>
</person>
<person>
<name>Anna</name>
<website url="www.example2.com">contact2</website>
<vote>80</vote>
</person>
</xml>

Thanks for help


Thanks for replay, I would like to obtain following RDF

 <rdf:Description rdf:about="http://www.example.com/xml">
<j.0:hasCritic>Joe</j.0:hasCritic>
     <rdf:Description rdf:about=Joe >
     <j.0:haswebsite>"www.example1.com"</j.0:haswebsite>
      <j.0:hascontact>contact1</j.0:hascontact>
      <j.0:hasvote>80</j.0:hasvote>
  </rdf:Description>
  <j.0:hasCritic>Anna</j.0:hasCritic>
     <rdf:Description rdf:about=Anna>
     <j.0:haswebsite>"www.example2.com"</j.0:haswebsite>
      <j.0:hascontact>contact2</j.0:hascontact>
      <j.0:hasvote>20</j.0:hasvote>
</rdf:Description>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Lucy
  • 471
  • 4
  • 12
  • 28

3 Answers3

4
You can use jena api for creating RDf model. Just parse xml file using dom parser and create Resourse , Property or Literal using Jena API. After creating this simply add into model.

Example:-
Model rdfModel = ModelFactory.createDefaultModel();
Resource resourse = rdfModel.createResource(Resourse Text);
Property property = rdfModel.createProperty(Property Text);
Literal literal = rdfModel.createLiteral(Literal Text);
resourse.addLiteral(property,literal);

Using Jena API you can store this model into rdf database(Triple).

Raxit
  • 242
  • 3
  • 11
  • Thanks Raxit for reply, using jena seems to do my work, but I am not sure how to use jena to create the RDFs from the file I have. since in the xml file each tag has three properties, I am a bit confused at this point. Could you please apply your example on my xml file? Thanks – Lucy May 08 '12 at 01:42
  • Hi sorry for my let reply. did u get the answer or still you want to populate above RDF file.. – Raxit Aug 26 '12 at 16:46
1

Grddl might be a workable approach, Jena has an implementation which is pretty straightforward to use. Otherwise, just a basic XSLT script could pretty easily transform that snippet into RDF. Hell, you could probably even just write a basic SAX listener and do the transformation there. There's no magical thing that will do it for you, you're going to have to put in some work, but there are options available.

Michael
  • 4,858
  • 19
  • 32
1
package tutorial;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class Test01 {
public static void main(String[] args) {
Model m = ModelFactory.createDefaultModel();
String NS = "<http://www.example.com/>";

Resource r1 = m.createResource( NS+"xml" );
Resource r2 = m.createResource( NS+"Joe" );
Resource r3 = m.createResource( NS+"Anna" );            
Property p1 = m.createProperty( NS+"hasCritic1" );
Property p2 = m.createProperty( NS+"hasCritic2" ); 
Property p3 = m.createProperty( NS+"hasWebsite" );
Property p4 = m.createProperty( NS+"hasContact" );
Property p5 = m.createProperty( NS+"hasVote" );

r1.addProperty(p1,r2);
r1.addProperty(p2,r3);
r2.addProperty(p3,"<http://www.example1.com>");
r2.addProperty(p4,"contact1");
r2.addProperty(p5,"80");
r3.addProperty(p3,"<http://www.example2.com>");
r3.addProperty(p4,"contact2");
r3.addProperty(p5,"20");
m.write( System.out );
} 
}