Creating nesting tags in a RDF file using Jena
I am new to Jena and I am now stuck with the following problem. Basically I need to create a RDF having nested elements, for ex开发者_StackOverflowample:
<rdf:description about="http://www.acme.com">
<dc:country>
<dc:capital>London</dc:capital>
<dc:language>English</dc:language>
<dc:country>
</rdf:description>
Maybe this is trivial, however I wasn't able to do that. I have read several tutorial but no example for this kind of element was present. Could some one provide me a working example in java capable to produce such output?
Many thanks in advance!
You don't say how you are generating your output, but the most likely explanation is that you are using the default output format RDF/XML
. If, instead, you call
yourModel.write( outStream, "RDF/XML-ABBREV" );
you will get a more compact structure that will most likely contain nested elements per your example.
There is a 'however', however.
You don't say why you "need to create RDF having nested elements". All of the output generated by Jena is semantically equivalent at the RDF triples level. Any compliant RDF processor will produce the same model by parsing a model serialised as RDF/XML, abbreviated or not, Turtle, n-triples, etc. So if you are handing the output off to another RDF process, it won't matter whether your output has nested structures or not.
If, however, you are hoping to feed the output into an XML toolchain then this can be more difficult. The RDF XML encoding is notoriously complex. Moreover, the Jena XML writer does not guarantee to serialise elements in a particular order. Depending on the contents of the Model
you are writing, the XML structure may come out looking quite different. This can make, for example, writing XSLT stylesheets tricky.
If you are planning to pipe RDF XML into your XML toolchain, I would recommend either creating a custom writer which produces reliable output in the format your XML tools can rely on, or perhaps querying your model using SPARQL and using the XML encoding of the SPARQL resultset as the input to your XML pipeline. SPARQL's XML encoding is more regular than full RDF/XML, and a better basis for subsequent XML processing.
精彩评论