从RDF节点拉出字符串 [英] Pull string from RDF node

查看:90
本文介绍了从RDF节点拉出字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与Jena一起使用SPARQL查询时,我试图以更易读的格式获取数据,但是我不知道如何以适当的方式提取数据. 到目前为止,输出为:

Im trying to get the data in a more readable format when using a SPARQL query with Jena, however I have no idea how to extract the data in a proper way. As for now, the output is:

" http://www.w3.org /TR/2003/PR-owl-guide-20031209/wine#SaucelitoCanyon "

相反,仅希望将"SaucelitoCanyon"作为输出.

Where instead would like to just have the "SaucelitoCanyon" as a output.

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();

    }

}

推荐答案

您可以使用.这将为您提供资源的本地名称,这似乎是您想要的:

You can use the "getLocalName" function in Jena. That will get you the local name of the resource, which is what you seem to be looking for:

QuerySolution row = results.next();
RDFNode winery = row.get("winery");
String r = winery.asNode().getLocalName();
System.out.println(r);

或者,您可以直接将节点作为资源获取,这样可以节省一行代码:

Alternatively you can get the node as a resource directly, which saves you one line of code:

String r = row.getResource("winery").getLocalName();

如以下评论所述,仅当您的前缀葡萄酒"与您在代码中声明的名称相同时,此答案才有效.否则结果可能不理想.

as stated in the comments below, this answer will only be valid if your prefix "wine" is as you have declared it in your code. Otherwise the results may not be as desired.

这篇关于从RDF节点拉出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆