我試圖在使用Jena的SPARQL查詢時以更易讀的格式獲取數據,但是我不知道如何以正確的方式提取數據。 至於現在,輸出結果是:從RDF節點拉字符串
「http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#SaucelitoCanyon」
如果不是想只是有「SaucelitoCanyon」作爲輸出。
public JenaQuery() {
String wineRegion = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "PREFIX wine:<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n"
+ "SELECT ?region ?winery \n"
+ "WHERE {?wine wine:locatedIn ?region . \n"
+ "?region wine:locatedIn wine:CaliforniaRegion . \n"
+ "?wine wine:hasMaker ?winery}";
String inputFileName = "wine.rdf";
// create an empty model
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in;
in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException(
"File: " + inputFileName + " not found");
}
// read the RDF/XML file
model.read(in, null);
try (QueryExecution qexec = QueryExecutionFactory.create(wineRegion, model)) {
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution row = results.next();
RDFNode winery = row.get("winery");
System.out.println(winery);
}
qexec.close();
}
}
您可以在SPARQL查詢中完成此操作,如[我的答案](http://stackoverflow.com/a/20845304/1281433)中所述[檢索owl中特定子類的類名](http ://stackoverflow.com/q/20830056/1281433)。 – 2014-10-16 13:54:50