简体   繁体   中英

Jena Model converts my RDF type explicit declaration to implicit and messes with the format

I have the following code that creates an RDF resource with some set properties and prints it on console.

    String uri = "http://krweb/";
    String name = "Giorgos Georgiou";
    String phone = "6976067554";
    String age = "27";
    String department = "ceid";
    String teaches = "java";

    Model model = ModelFactory.createOntologyModel();
    model.setNsPrefix("krweb", uri);

    Resource giorgosgeorgiou = model.createResource(uri+name.toLowerCase().replace(" ", ""), model.createResource(uri+"Professor"));

    Property has_name = model.createProperty(uri+"has_name");
    Property has_phone = model.createProperty(uri+"has_phone");
    Property has_age = model.createProperty(uri+"has_age");
    Property member_of = model.createProperty(uri+"member_of");
    Property teach = model.createProperty(uri+"teaches");

    giorgosgeorgiou.addProperty(teach, model.createResource(uri+teaches));
    giorgosgeorgiou.addProperty(member_of, model.createResource(uri+department));
    giorgosgeorgiou.addProperty(has_age,age);
    giorgosgeorgiou.addProperty(has_phone,phone);
    giorgosgeorgiou.addProperty(has_name,name);
   //giorgosgeorgiou.addProperty(RDF.type, model.createResource(uri+"Professor"));
    model.write(System.out,"RDF/XML");

I want the model printed in this format:

<rdf:Description rdf:about="http://krweb/giorgosgeorgiou">
    <rdf:type rdf:resource="http://krweb/Professor"/>
    <krweb:has_name>Giorgos Georgiou</krweb:has_name>
    <krweb:has_phone>6976067554</krweb:has_phone>
    <krweb:has_age>27</krweb:has_age>
    <krweb:member_of rdf:resource="http://krweb/ceid"/>
    <krweb:teaches rdf:resource="http://krweb/java" />
</rdf:Description>

Instead I get this:

  <krweb:Professor rdf:about="http://krweb/giorgosgeorgiou">
    <krweb:has_name>Giorgos Georgiou</krweb:has_name>
    <krweb:has_phone>6976067554</krweb:has_phone>
    <krweb:has_age>27</krweb:has_age>
    <krweb:member_of rdf:resource="http://krweb/ceid"/>
    <krweb:teaches rdf:resource="http://krweb/java"/>
  </krweb:Professor>

Somehow, the rdf type property gets converted to some implicit declaration and is presented in what I suppose is a "pretty" format. Is there a way to bypass this?

Internally the RDF data is held as triples - no knowledge of how they were formatted on input is stored.

The default output is pretty RDF/XML.

To get the plain, flat format use RDFFormat.RDFXML_PLAIN

RDFDataMgr.write(System.out, model, RDFFormat.RDFXML_PLAIN);

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