How to convert RDF to string

I am creating some RDF file with the JENA library: Model model = ModelFactory.createDefaultModel();... now how can I convert this to a string?

THX

+3
source share
2 answers

Try something like this:

String syntax = "RDF/XML-ABBREV"; // also try "N-TRIPLE" and "TURTLE"
StringWriter out = new StringWriter();
model.write(out, syntax);
String result = out.toString();

It uses Jena's built-in authors, who can already output RDF graphics in various supported RDF syntaxes, such as RDF / XML and Turtle and N-Triples. If you just want to reset to System.out, then this is even easier:

model.write(System.out, "RDF/XML-ABBREV");
+11
source

RDF . , , ... RDF " Triple", : Subject (Resource), () (RDFNode - ). toString , :

// list the statements in the Model
StmtIterator iter = model.listStatements();

// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
    Statement stmt      = iter.nextStatement();  // get next statement
    Resource  subject   = stmt.getSubject();     // get the subject
    Property  predicate = stmt.getPredicate();   // get the predicate
    RDFNode   object    = stmt.getObject();      // get the object

    System.out.print(subject.toString());
    System.out.print(" " + predicate.toString() + " ");
    if (object instanceof Resource) {
       System.out.print(object.toString());
    } else {
        // object is a literal
        System.out.print(" \"" + object.toString() + "\"");
    }

    System.out.println(" .");
} 

toString .

/: Jena - RDF ( "" ).

0

All Articles