converting resultset to RDF/XML in Jena

2019-02-24 21:20发布

问题:

I'm trying to convert a resultset in XML/RDF format but with this code:

ResultSet result = rmParliament.selectQuery(select);
System.out.println(ResultSetFormatter.asText(result));
ResultSetFormatter.outputAsRDF(System.out, "RDF/XML", result);

The second line of the code is to verify the correct behaviour of the query (it works!), but I get in the console the following output:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rs="http://www.w3.org/2001/sw/DataAccess/tests/result-set#" > 
 <rdf:Description rdf:nodeID="A0">
    <rs:size rdf:datatype="http://www.w3.org/2001/XMLSchema#int">0</rs:size>
    <rs:resultVariable>value</rs:resultVariable>
    <rs:resultVariable>property</rs:resultVariable>
    <rs:resultVariable>name</rs:resultVariable>
    <rdf:type rdf:resource="http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet"/>
 </rdf:Description>
</rdf:RDF>

Is doesn't contain my data, what's wrong with my code?

回答1:

The problem is that your print debugging actually consumes all the results, leaving the ResultSet at the end. There are no more results available when you try to output it as RDF/XML.

You can fix it by making the ResultSet rewindable:

ResultSetRewindable result =
    ResultSetFactory.makeRewindable( rmParliament.selectQuery(select) );
System.out.println(ResultSetFormatter.asText(result));
result.reset(); // back to the start
ResultSetFormatter.outputAsRDF(System.out, "RDF/XML", result);


标签: java xml rdf jena