简体   繁体   中英

Reading RDF data using Jena failing

The following code is to read a schema and a data file to find rdf.type of colin and Person. However, I am getting the error:

Exception in thread "main" org.apache.jena.riot.RiotException: [line: 1, col: 1 ] Content is not allowed in prolog. The code is given below:

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);
} 

}

The file rdfsDemoData.rdf

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

The file 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>

Your data is bad syntax. You are mixing Turtle and RDF/XML. RDF/XML does nto have @prefix - it uses XML's namespaces. It looks like you want an XML entity declaration like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM