Aram
Aram

Reputation: 113

How to write a Jena QuerySolution result to RDF/XML file?

I need to export/write the .ttl QuerySolution results to RDF/XML file.

I have tried the code below, but I am getting the following error with RDFDataMgr.write:

The method write(OutputStream, Model, Lang) in the type RDFDataMgr is not applicable for the arguments (OutputStream, QuerySolution, Lang)

Query query = QueryFactory.create(queryString);
QueryExecution qexec= QueryExecutionFactory.create(query, model2);
try {
    ResultSet resultat= qexec.execSelect();
    while (resultat.hasNext()) {
        QuerySolution sol=resultat.nextSolution();

        String outfile = "/auto_home/rdftest/outfile.rdf";
        OutputStream out = new FileOutputStream(outfile);

        RDFDataMgr.write(out, sol, Lang.RDFXML); 
    }
} finally {
    qexec.close(); 
}

Upvotes: 0

Views: 287

Answers (1)

cygri
cygri

Reputation: 9472

SPARQL supports two main kinds of queries: SELECT queries and CONSTRUCT queries.

SELECT queries return a table of solutions. You are running a SELECT query.

CONSTRUCT queries create a new RDF graph from the solutions.

Turtle and RDF/XML are formats for RDF graphs. They are not formats for solution tables. Therefore, you can only write the results of CONSTRUCT queries to these formats.

So you can change your query to a CONSTRUCT query and use the proper APIs for executing them (execConstruct() returns a Model instead of ResultSet), or else use ResultSetFormatter to write the entire solution table (not each individual row of the table as you are trying to do) to one of the formats that exist for that purpose: JSON, CSV, TSV, XML.

Upvotes: 1

Related Questions