2015-11-02 87 views
-1

以下代碼是讀取模式和數據文件以查找colin和Person的rdf.type。但是,我收到錯誤:使用Jena失敗讀取RDF數據

線程「main」org.apache.jena.riot.RiotException中的異常:[line:1,col:1]在prolog中不允許使用內容。代碼如下:

public void reason(){ 
    String NS = "urn:x-hp:eg/"; 

    String fnameschema = "D://Work//EclipseWorkspace//Jena//data//rdfsDemoSchema.rdf"; 
    String fnameinstance = "D://Work//EclipseWorkspace//Jena//data//rdfsDemoData.rdf"; 

Model schema = FileManager.get().loadModel(fnameschema); 
Model data = FileManager.get().loadModel(fnameinstance); 

InfModel infmodel = ModelFactory.createRDFSModel(schema, data); 

Resource colin = infmodel.getResource(NS+"colin"); 

System.out.println("Colin has types"); 

for (StmtIterator i = infmodel.listStatements(colin, RDF.type, (RDFNode)null); i.hasNext();) { 
     Statement s = i.nextStatement(); 
     System.out.println(s); 
} 

Resource Person = infmodel.getResource(NS+"Person"); 
System.out.println("\nPerson has types:"); 
for (StmtIterator i = infmodel.listStatements(Person, RDF.type, (RDFNode)null); i.hasNext();) { 
    Statement s = i.nextStatement(); 
    System.out.println(s); 
} 

}

文件rdfsDemoData.rdf

@prefix eg: <urn:x-hp:eg/> . 
<Teenager rdf:about="&eg;colin"> 
    <mum rdf:resource="&eg;rosy" /> 
    <age>13</age> 
</Teenager> 

文件rdfsDemoSchema.rdf

@prefix eg: <urn:x-hp:eg/> . 

<rdf:Description rdf:about="&eg;mum"> 
    <rdfs:subPropertyOf rdf:resource="&eg;parent"/> 
</rdf:Description> 

<rdf:Description rdf:about="&eg;parent"> 
    <rdfs:range rdf:resource="&eg;Person"/> 
    <rdfs:domain rdf:resource="&eg;Person"/> 
</rdf:Description> 

<rdf:Description rdf:about="&eg;age"> 
    <rdfs:range rdf:resource="&xsd;integer" /> 
</rdf:Description> 
+0

不清楚爲什麼這個問題已被投票。澄清將有助於未來。 – Bonson

回答

0

你的數據是錯誤的語法。你在混合Turtle和RDF/XML。 RDF/XML沒有@prefix - 它使用XML的名稱空間。它看起來像你想要一個XML實體聲明,如:

<?xml version="1.0"?> 
<!DOCTYPE rdf:RDF [ 
    <!ENTITY eg "urn:x-hp:eg/" > 
]> 
... 
+0

這工作: <!DOCTYPE的rdf:RDF [! ]> <的rdf:RDF的xmlns:RDF =「http://www.w3.org/1999/02/22-rdf-syntax-ns#」 xmlns:foaf =「http://xmlns.com/foaf/0.1/」 xmlns:rel =「 http://some.namespace.for.rel.com/ns/「 xmlns:rdfs =」http://www.w3.org/2000/01/rdf-schema#「> Bonson