jenafoaf

Extracting FOAF information from Jena


I'm new here,and i have some problem about FOAF. I use jena create a FOAF like this :

<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
    <foaf:phone>12312312312</foaf:phone>
    <foaf:nick>L</foaf:nick>
    <foaf:name>zhanglu</foaf:name>

But i want to the FOAF shows like this:

<foaf:Person>
    <foaf:phone>12312312312</foaf:phone>
    <foaf:nick>L</foaf:nick>
    <foaf:name>zhanglu</foaf:name>
</foaf:Person>

What can i do?

this is my source code:

Model m = ModelFactory.createDefaultModel();
m.setNsPrefix("foaf", FOAF.NS);

Resource r = m.createResource(NS);
r.addLiteral(FOAF.name, "zhanglu");
r.addProperty(FOAF.nick, "L");
r.addProperty(FOAF.phone, "123123123");
r.addProperty(RDF.type, FOAF.Person);

FileOutputStream f = new FileOutputStream(fileName);
m.write(f);

who can tell me? thanks


Solution

  • First thing to say is that the two forms that you quote have exactly the same meaning to RDF - that is, they produce exactly the same set of triples when parsed into an RDF graph. For this reason, it's generally not worth worrying about the exact syntax of the XML produced by the writer. RDF/XML is, in general, not a friendly syntax to read. If you just want to serialize the Model, so that you can read it in again later, I would suggest Turtle syntax as it's more compact and easier for humans to read and understand.

    However, there is one reason you might want to care specifically about the XML serialization, which is if you want the file to be part of an XML processing pipeline (e.g. XSLT or similar). In this case, you can produce the format you want by changing the last line of your example:

    m.write( f, "RDF/XML-ABBREV" );
    

    or, equivalently,

    m.write( f, FileUtils.langXMLAbbrev );