Reputation: 16525
I create some RDF file wih JENA library: Model model = ModelFactory.createDefaultModel();
...
now how I can I convert this to string ?
thx
Upvotes: 4
Views: 4321
Reputation: 9472
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();
This uses Jena's built-in writers that can already output RDF graphs in the various supported RDF syntaxes, such as RDF/XML and Turtle and N-Triples. If you just want to dump to System.out
, then it's even easier:
model.write(System.out, "RDF/XML-ABBREV");
Upvotes: 14
Reputation: 2241
A RDF model is a set of Statements. Every time you add a property to the model, a Statement is created...and a Statement in RDF model is also called as a 'Triple', coz it has 3 parts: Subject (Resource), Predicate (Property) and Object (RDFNode - can be a Resource). You can call the toString method on these objects, as shown in the code below:
// 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(" .");
}
Note the 'toString' method in the above code.
Source/Ref: Jena - RDF (Check the 'Statements' section).
Upvotes: 0