2

I'm wondering how to create a .txt or .xls file output from the result of Jena SPARQL query. Can anyone help?

here some part of the code

InputStream in = new FileInputStream(new File("./src/myfile.owl"));

                Model model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, null);
                model.read(in, null);
                in.close();

                String queryString = 
                        "prefix : <http://www.semanticweb.org/thato/ontologies/2012/10/9/thesis_ontology_1try#> "  +    
                        "prefix rdfs: <" + RDFS.getURI() + "> "           +
                        "prefix owl: <" + OWL.getURI() + "> "             +
                        "prefix rdf: <" + RDF.getURI() + "> "             +
                        "select  ?Model  " +
                        "where {" +
                        "{?Model rdf:type :ApplicationModel. "      +
                        "?Model :hasDomain ?domain.    "                  +
                        "?domain :domainCode ?domaincode. "               +
                        "FILTER (?domaincode =" + "'"+ domainElement.getAttribute(attrDomain) + "'"+ ")"               +
                        "?Model :hasPhase ?phase." +
                        "?phase :name ?phasecode. "               +
                        "FILTER (?phasecode = "+ "'"+ phaseElement.getAttribute(attrPhase)+"'"+")"  +
                        "?Model :hasLevelOfDetail ?lod." +
                        "?lod :name ?lodcode."               +
                        "FILTER (?lodcode = "+ lodElement.getAttribute(attrLOD) +")"    +
                        "}"
                        ;
                Query query = QueryFactory.create(queryString);
                QueryExecution qe = QueryExecutionFactory.create(query, model);
                ResultSet results = qe.execSelect();
                ResultSetFormatter.out(System.out, results, query);
                qe.close();

        }
    }
blackSweet
  • 131
  • 1
  • 11

1 Answers1

6

You haven't really explained what you want the format of the output to look like but I will have a stab at answering regardless.

Jena will produce a text based serialization of results which is ASCII table style, you can get this with the following:

ResultSetFormatter.out(System.out, results, query);

This is what your code already does so I'll assume that this format is not satisfactory for you?

You can't generate an Excel spreadsheet directly but you can generate CSV which can of course be imported into Excel:

ResultSetFormatter.outputAsCSV(System.out, results);

The CSV serialization is however a lossy serialization and it does not shorten URIs into abbreviated forms - see SPARQL 1.1 Query Results CSV and TSV for the specification of this.

If you want to write to an actual file simply switch System.out for the appropriate OutputStream implementation e.g. FileOutputStream

RobV
  • 26,016
  • 10
  • 71
  • 114
  • Thanks...you gave me some idea how to make it!! I added PrintStream out = new PrintStream(new FileOutputStream("./src/output.txt")); System.setOut(out); ResultSetFormatter.out(out, results, query); – blackSweet Jan 30 '13 at 12:12
  • 1
    You do know you can pass the `FileOutputStream` directly, right? There is no need to wrap in a `PrintStream` and change the stdout stream – RobV Jan 30 '13 at 18:22