Skip to content

Converting an iterator over statements to ntriples #4365

Answered by frensjan
D063520 asked this question in Q&A
Discussion options

You must be logged in to vote
Iterator<Statement> statements = ...;

StringWriter sw = new StringWriter();
RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, sw);
writer.startRDF();
statements.forEachRemaining(writer::handleStatement);
writer.endRDF();
String ntriples = sw.toString();

This would give you a string with all the statements from the iterator. If you must have a string per statement, you would probably need to create a writer per statement then.

Iterator<Statement> statements = ...;

while (statements.hasNext()) {
    StringWriter sw = new StringWriter();
    RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, sw);
    writer.startRDF();
    writer.handleStatement(statements.next());
    writer.en…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@D063520
Comment options

Answer selected by hmottestad
Comment options

You must be logged in to vote
1 reply
@D063520
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants