I want to parse an RDF file which is in n-triple form.
I can write my own parser but I would rather use a library, and Jena seems unecessarily complicated for this purpose (or at least I can't see their docs explaining how to read n-triples in a sensible way).
Could you please either point me to any useful libraries or if you know either Sesame or Jena well, you might know something about how they can solve this.
With Jena it is not so difficult:
Given a file rdfexample.ntriple
containing the following RDF in N-TRIPLE form (example taken from here):
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#year> "1988" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#price> "9.90" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#company> "CBS Records" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#country> "UK" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#artist> "Bonnie Tyler" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#year> "1985" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#price> "10.90" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#company> "Columbia" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#country> "USA" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#artist> "Bob Dylan" .
the following code
public static void main(String[] args) {
String fileNameOrUri = "src/a/rdfexample.ntriple";
Model model = ModelFactory.createDefaultModel();
InputStream is = FileManager.get().open(fileNameOrUri);
if (is != null) {
model.read(is, null, "N-TRIPLE");
model.write(System.out, "TURTLE");
} else {
System.err.println("cannot read " + fileNameOrUri);;
}
}
reads the file, and prints it out in TURTLE form:
<http://www.recshop.fake/cd/Hide your heart>
<http://www.recshop.fake/cd#artist>
"Bonnie Tyler" ;
<http://www.recshop.fake/cd#company>
"CBS Records" ;
<http://www.recshop.fake/cd#country>
"UK" ;
<http://www.recshop.fake/cd#price>
"9.90" ;
<http://www.recshop.fake/cd#year>
"1988" .
<http://www.recshop.fake/cd/Empire Burlesque>
<http://www.recshop.fake/cd#artist>
"Bob Dylan" ;
<http://www.recshop.fake/cd#company>
"Columbia" ;
<http://www.recshop.fake/cd#country>
"USA" ;
<http://www.recshop.fake/cd#price>
"10.90" ;
<http://www.recshop.fake/cd#year>
"1985" .
So, with Jena you can easily parse RDF (in any form) into a com.hp.hpl.jena.rdf.model.Model
object, which allows you to programmatically manipulate it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With