如何将Apache Jena QuerySolution转换为Java对象?

时间:2019-05-04 23:36:53

标签: java rdf jena

我想将整个ResultSet转换为Person对象。通过运行下面的代码,我仅获得人的名字,但是我还想从电话/描述中访问rdf:resource以便在Person构造函数中使用它们。 rdf数据的结构如下:

<rdf:RDF
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
      xmlns:foaf="http://xmlns.com/foaf/0.1/"
      xmlns:admin="http://webns.net/mvcb/">
<foaf:PersonalProfileDocument rdf:about="">
  <foaf:maker rdf:resource="#me"/>
  <foaf:primaryTopic rdf:resource="#me"/>
  <admin:generatorAgent rdf:resource="http://www.ldodds.com/foaf/foaf-a-matic"/>
  <admin:errorReportsTo rdf:resource="mailto:leigh@ldodds.com"/>
</foaf:PersonalProfileDocument>
<foaf:Person rdf:ID="me">
<foaf:name>Admin Admin</foaf:name>
<foaf:givenname>Admin</foaf:givenname>
<foaf:family_name>Admin</foaf:family_name>
<foaf:mbox_sha1sum>ba76ee932f6bd67888bf6bd94790ec76ed916f91</foaf:mbox_sha1sum>
<foaf:depiction rdf:resource="img1.jpg"/>
<foaf:phone rdf:resource="tel:876698"/>
<foaf:workplaceHomepage rdf:resource="Company"/>
<foaf:workInfoHomepage rdf:resource="Administrator"/>
<foaf:knows>
<foaf:Person>
<foaf:name>Dan</foaf:name>
<foaf:depiction rdf:resource="img1.jpg"/>
<foaf:phone rdf:resource="tel:7565767"/>
<foaf:workplaceHomepage rdf:resource="EventCompany"/>
<foaf:workInfoHomepage rdf:resource="CEO"/>
<foaf:mbox_sha1sum>b6244a6188500461fe5dae3bc55bbd6811fd4452</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="Jane"/></foaf:Person></foaf:knows>
...

这是我到目前为止尝试过的:

FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model = FileManager.get().loadModel("D:\\Project\\src\\main\\resources\\data.rdf");
        String queryString ="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
                       "PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
                       "SELECT * WHERE { "+
                       "?person foaf:name ?x ."+
                       "FILTER(?x = \"Dan\")" +
                       "}";

        Query query = QueryFactory.create(queryString);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        ResultSet resultSet = qexec.execSelect();
        while (resultSet.hasNext()){
            QuerySolution soln = resultSet.nextSolution();
            Literal name = soln.getLiteral("x");
            System.out.println(name);
        }

0 个答案:

没有答案