1

RDF4J Newbie question -

When I load this RDF from file:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<foaf:Person>
 <foaf:name>My Name</foaf:name>
</foaf:Person>

</rdf:RDF>

using this code

String fileName = "foaf.rdf";
try {
    InputStream is = new FileInputStream(fileName);
    Model model = Rio.parse(is, "", RDFFormat.RDFXML);
    System.out.println();
    Rio.write(model, System.out, RDFFormat.RDFXML);
    System.out.println();
    is.close();
}
catch (Exception ex) {
    System.out.println(ex.toString());
}

the output looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:nodeID="node1basf0r6vx1">
    <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
    <foaf:name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">My Name</foaf:name>
</rdf:Description>

</rdf:RDF>

How would I go about getting RDF printed out in the original format?

kgu87
  • 2,050
  • 14
  • 12
  • What is your use case? Why would you need to get the exact same format back? – ChristophE Mar 13 '17 at 15:32
  • A WYSIWYG ontology editor. If user uploads a document in a valid certain format, they want to see it back in the same format. – kgu87 Mar 13 '17 at 15:43
  • Will be a bit hard. You provided here a foaf:person but without any real identifier like a URI. RDF4J creates a BNode to handle your data and thats why you get this slightly changed file. And if it is a WYSIWIG Ontology editor like Protegé I don't see why your user need to see the file. – ChristophE Mar 14 '17 at 08:13
  • Thanks! Wasn't sure if I'm missing anything, looks like I'm not:) Unfortunately, this has to be completely browser based, so Protege isn't an option. – kgu87 Mar 14 '17 at 18:18

1 Answers1

2

RDF4J has also a pretty print function. Maybe this can help you with your output.

You just have to create a writer and then set pretty print to true.

Rio.createWriter(format, outputStream).set(BasicWriterSettings.PRETTY_PRINT, true)
ChristophE
  • 760
  • 1
  • 9
  • 21