I want to serialise resources individually with Jena. I've retrieved a list of resources of the type ont:Artist, and I would like to generate individual serialiations for each one, but which also contain the full descriptions of the resource's objects. I'm currently copying each resource into a new blank model and serialising it. For example, I am currently getting:

<ont:Artist rdf:about="http://example.com/artist/77">
    <ont:hasAlbum rdf:resource="http://example.com/album/75"/>

With the album only represented by its URI. I would like all of the details about album to be included in this serialisation, like so:

<ont:Artist rdf:about="http://example.com/artist/77">
         <iweb:Album rdf:about="http://example.com/artist/77">
             <iweb:hasTitle>This is a title</iweb:hasTitle>

How can I do this for an individual resource?

asked 20 May '11, 12:38

blork's gravatar image

accept rate: 0%

I'm no expert on the Jena API, but I assume you can simply fire a SPARQL CONSTRUCT query and treat/process the result as a new Model. The following SPARQL query will give you back all artists, their album, and all the properties of each album:

   ?artist a ont:Artist ;
           iweb:hasAlbum ?album . 
   ?album ?P ?Y .
   ?artist a ont:Artist ;
           iweb:hasAlbum ?album . 
   ?album ?P ?Y .

If you want to do this for an invidual artist, replace ?artist with the URI of the individual (or add a filter condition).

permanent link

answered 22 May '11, 22:15

Jeen%20Broekstra's gravatar image

Jeen Broekstra ♦
accept rate: 37%

edited 22 May '11, 22:16

Resource individual = yourModel.createResource(individualURI);
Model individualModel = ModelFactory.createDefaultModel();
traverseModel(yourModel, individualModel, individual);
individualModel.write(outputStream, "RDF/XML-ABBREV");

public void traverseModel (Model completeModel, Model newModel, Resource node) {
    StmtIterator subNodes = completeModel.listStatements(node, null, null);
    while (subNodes.hasNext()) {
        Statement subStmt = subNodes.next();
        if (subStmt.getObject().isResource() && ! newModel.listStatements(subStmt.getObject(), null, null).hasNext()) traverseModel(completeModel, newModel, subStmt.getObject());
permanent link

answered 23 May '11, 03:51

William%20Greenly's gravatar image

William Greenly
accept rate: 13%

edited 23 May '11, 14:19


Nice piece of code! I take it that the last code-line should call 'traverseModel' instead of 'traverse', right?

(23 May '11, 09:28) Ryan Kohl Ryan%20Kohl's gravatar image

Yes, thanks Ryan. Will make appropriate edit.

(23 May '11, 09:34) William Greenly William%20Greenly's gravatar image

You might want to add something to guard against infinite loops where you have cycles in the RDF graph.

(23 May '11, 10:12) mhgrove mhgrove's gravatar image

Yeah - that would be a good idea (cringe). Will make the appropriate edit.

(23 May '11, 14:03) William Greenly William%20Greenly's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 20 May '11, 12:38

question was seen: 1,710 times

last updated: 23 May '11, 14:19