Jena解析rdf内容错误

时间:2017-01-15 19:55:50

标签: java rdf jena ontology

我实现了一个代码来解析rdf代码中的一些信息,但是它会引发以下错误:

  

线程“main”中的异常com.hp.hpl.jena.query.QueryParseException:第1行,第126列:未解析的前缀名称:foaf:name

导致此错误的问题是什么?

这是我的代码:

import java.util.Iterator;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class Main {

    public static void main(String args[])
    {
        sparqlTest();
    }

    static void sparqlTest()
    {
        FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model = FileManager.get().loadModel("c:/Users/Laura/workspace/jena-app/src/data.rdf");

        String queryString = 
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
                "PREFIX foat: <http://xmlns.com/foaf/0.1/> " +
                "SELECT * WHERE { " +
                " ?person foaf:name ?x ." +
                "}";
        Query query = QueryFactory.create(queryString);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        try {

            ResultSet results = qexec.execSelect();
            while (results.hasNext() ) {
                QuerySolution soln = results.nextSolution();
                Literal name = soln.getLiteral("x");
                System.out.println(name);
            }
        } finally {
            qexec.close();
        }

    }

}

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>George V</foaf:name>
<foaf:title>Dr</foaf:title>
<foaf:givenname>George</foaf:givenname>
<foaf:family_name>V</foaf:family_name>
<foaf:nick>jorch</foaf:nick>
<foaf:mbox_sha1sum>b01b5835fa8ae7b7582968a7ecacb9b85503a6c9</foaf:mbox_sha1sum>
<foaf:homepage rdf:resource="betacoding.net"/>
<foaf:phone rdf:resource="tel:123456"/>
<foaf:workplaceHomepage rdf:resource="work"/>
<foaf:workInfoHomepage rdf:resource="development"/>
<foaf:schoolHomepage rdf:resource="a school"/>
<foaf:knows>
<foaf:Person>
<foaf:name>John</foaf:name>
<foaf:mbox_sha1sum>c7856d7a98889cee78a21245301a560e8f74d191</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="Charlie"/></foaf:Person></foaf:knows>
<foaf:knows>
<foaf:Person>
<foaf:name>Mary</foaf:name>
<foaf:mbox_sha1sum>a61c03838106b21fc083e7bc65e76c511c549d22</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="Margaret"/></foaf:Person></foaf:knows>
<foaf:knows>
<foaf:Person>
<foaf:name>Charlie</foaf:name>
<foaf:mbox_sha1sum>27f94c268f1a1c6004be361f4045d43c3745c0de</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="John"/></foaf:Person></foaf:knows></foaf:Person>
</rdf:RDF>

这些应该是预期的结果:

Charlie
Mary
John
George

1 个答案:

答案 0 :(得分:3)

您收到错误是因为您已在下面的查询中将其指向foat而不是foaf

"PREFIX foat: <http://xmlns.com/foaf/0.1/>"

这应该改变如下:

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>"

Change

您更新的查询应如下所示:

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 ." +
        "}";