Java Jena解析rdf内容错误
我实现了一个从rdf代码中解析一些信息的代码,但它引发了以下错误: 线程“main”com.hp.hpl.jena.query.QueryParseException中的异常:第1行第126列:未解析的前缀名称:foaf:name 导致此错误的问题是什么 这是我的代码:Java Jena解析rdf内容错误,java,rdf,jena,ontology,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; impor
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();
}
}
}
您得到错误是因为您在下面的查询中将其指向了
foat
,而不是foaf
"PREFIX foat: <http://xmlns.com/foaf/0.1/>"
“前缀foat:”
这应作如下更改:
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
“前缀foaf:”
更新后的查询应如下所示:
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 ." +
"}";
字符串查询字符串=
“前缀rdf:”+
“前缀foaf:”+
“选择*其中{”+
“人员foaf:姓名?x”+
"}";
可能是因为您在前缀foat:
中指定了foat
,而不是foaf
!Daaannnggg。。。。谢谢,这个修好了!
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
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 ." +
"}";