Semweb4j/filesQueries/step2

From semanticweb.org
Jump to: navigation, search

Up | Previous step | Next step

Contents

[edit] Step 2: Serialization syntaxes

RDF2Go offers different syntaxes for serialization of a RDF model. You could choose to serialize in one syntax for this tool and another syntax for that tool to post-process your RDF models.

[edit] N3

Now, we will write to System.out:

OutputStreamWriter writer = new OutputStreamWriter(System.out);
model.writeTo( writer, Syntax.Ntriples);

Model.writeTo can be called with a StreamWriter (like above) or with an OutStream (see below).

[edit] RDF/XML

model.writeTo( writer, Syntax.RdfXml);

[edit] Turtle

model.writeTo( writer, Syntax.Turtle);

[edit] TriG

model.writeTo( writer, Syntax.Trig);

[edit] TriX

Because Trix is not implemented in Jena 2.4, we don't do this:

model.writeTo( writer, Syntax.Trix);

[edit] using a String

You can easily serialize into a String like this:

String turtleSerialization = model.serialize(Syntax.Turtle);

[edit] the complete program

package org.ontoware.semweb4j.lessons.lesson2;

import java.io.IOException;
import java.io.OutputStreamWriter;

import org.ontoware.rdf2go.RDF2Go;
import org.ontoware.rdf2go.exception.ModelRuntimeException;
import org.ontoware.rdf2go.model.Model;
import org.ontoware.rdf2go.model.Syntax;
import org.ontoware.rdf2go.model.node.URI;

public class Step2 {

Now we skip some code as it's the same as in step 1...

After adding statements to our model:

		// serializing model with every syntax possible
		OutputStreamWriter writer = new OutputStreamWriter(System.out);
		writer.write("Ntriples following:\n");
		model.writeTo( writer, Syntax.Ntriples);
		writer.write("\nRdfXml following:\n");
		model.writeTo( writer, Syntax.RdfXml);
		writer.flush();

		// it works also with System.out (which is an OutStream):
		writer.write("\nTurtle following:\n");
		model.writeTo(System.out, Syntax.Turtle);
		writer.write("\nTrig following:\n");
		model.writeTo(System.out, Syntax.Trig);
		
		// because Trix is not implemented in Jena24, we don't do this:
		// model.writeTo( writer, Syntax.Trix);

		// It's also possible to serialize into a String:
		String turtleSerialization = model.serialize(Syntax.Turtle);

[edit] output

The output of this program is:

Ntriples following:
<http://xam.de/foaf.rdf.xml#i> <http://xmlns.com/foaf/0.1/#term_name> "Max V\u00F6lkel" .
<http://xam.de/foaf.rdf.xml#i> <http://xmlns.com/foaf/0.1/#term_currentProject> <http://semweb4j.org> .

RdfXml following:
<rdf:RDF
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:j.0="http://xmlns.com/foaf/0.1/#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
  <rdf:Description rdf:about="http://xam.de/foaf.rdf.xml#i">
    <j.0:term_name>Max Völkel</j.0:term_name>
    <j.0:term_currentProject rdf:resource="http://semweb4j.org"/>
  </rdf:Description>
</rdf:RDF>

Turtle following:
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf:     <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf:    <http://xmlns.com/foaf/0.1/> .
@prefix :        <#> .

<http://xam.de/foaf.rdf.xml#i>
      <http://xmlns.com/foaf/0.1/#term_currentProject>
              <http://semweb4j.org> ;
      <http://xmlns.com/foaf/0.1/#term_name>
              "Max Völkel" .

Note that there will be a text like this before your output:

01.01.2007 12:34:56 org.ontoware.rdf2go.RDF2Go checkModelFactory
INFO: Using ModelFactory 'class org.ontoware.rdf2go.impl.jena24.ModelFactoryImpl' which was loaded via org.ontoware.rdf2go.impl.StaticBinding.

if you don't specify the adapter (/ ModelFactoryImpl / triple store).

We will never mention these 2 lines if they appear in the output, so remember they don't indicate bugs, warnings, etc..

Sorry that Trig syntax is not listed in output here, that's coming soon.

Personal tools
Namespaces

Variants
Actions
Navigation
services
Toolbox