我正在使用APACHE JENA(2.9.4版本)使用SPARQL Query获取结果。以下是我正在使用的代码。
public class DataGeneratorTest {
public static void main(String[] args) {
String query = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX owl:<http://dbpedia.org/ontology/> select ?y where { ?x a owl:Person; rdfs:label ?y. FILTER(LANG(?y) = \"en\") } LIMIT 77";
ResultSet resultSet = SparqlUtil.generate(query);
ResultSetFormatter.out(System.out, resultSet);
}
}
我有一个实用程序类来执行sparql查询......
public class SparqlUtil {
public static ResultSet generate(String argEndPoint, String argQuery) {
Precondition.checkNotEmpty(argQuery);
String endPoint = argEndPoint;
if (Precondition.checkEmpty(endPoint)) {
endPoint = SparqlConstants.SPARQL_ENDPOINT;
}
QueryExecution queryExecution = null;
try {
Query query = QueryFactory.create(argQuery);
queryExecution = QueryExecutionFactory.sparqlService(endPoint,
query);
return queryExecution.execSelect();
} finally {
if (Precondition.checkNotNull(queryExecution)) {
queryExecution.close();
}
}
}
public static ResultSet generate(String argQuery) {
return generate(SparqlConstants.SPARQL_ENDPOINT, argQuery);
}
}
当我运行此代码时,我收到以下异常。
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[40,26]
Message: XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.getOneSolution(XMLInputStAX.java:435)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.hasNext(XMLInputStAX.java:232)
at com.hp.hpl.jena.sparql.resultset.ResultSetMem.<init>(ResultSetMem.java:95)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:147)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:130)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:118)
at com.hp.hpl.jena.sparql.resultset.TextOutput.format(TextOutput.java:65)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:122)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:74)
at com.data.generator.test.DataGeneratorTest.main(DataGeneratorTest.java:17)
Exception in thread "main" com.hp.hpl.jena.sparql.resultset.ResultSetException: XMLStreamException: ParseError at [row,col]:[40,26]
Message: XML document structures must start and end within the same entity.
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.staxError(XMLInputStAX.java:539)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.hasNext(XMLInputStAX.java:236)
at com.hp.hpl.jena.sparql.resultset.ResultSetMem.<init>(ResultSetMem.java:95)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:147)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:130)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:118)
at com.hp.hpl.jena.sparql.resultset.TextOutput.format(TextOutput.java:65)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:122)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:74)
at com.data.generator.test.DataGeneratorTest.main(DataGeneratorTest.java:17)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[40,26]
Message: XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.getOneSolution(XMLInputStAX.java:435)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.hasNext(XMLInputStAX.java:232)
... 8 more
我正在尝试仅检索前77个结果。当我将此结果集限制为76时,我能够无任何异常地获取结果,但是当我将此限制增加到77时,我得到了上述异常。第77条记录存在问题。我怎样才能克服这个问题。我卡在这里。任何人都可以帮我这个...... ???
Thanx提前, 阿玛尔。
答案 0 :(得分:1)
什么是SparqlConstants.SPARQL_ENDPOINT? dbpedia.org有时间限制并截断结果,导致非法XML。这可以解释这种情况但不知道第77条是什么,如果有的话,我们不能说。