title: Parsing RDF recursive with Jena
body: <p>I'm trying to parse a RDF document recursive using Apache Jena. It consists out of datasets like this: </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dcat:dataset&gt;
      &lt;dcat:Dataset rdf:about="http://url/" &gt; 
          &lt;dct:description xml:lang="ca"&gt;Description&lt;/dct:description&gt;
          &lt;dct:license rdf:resource="http://creativecommons.org/licenses/by/3.0/"/&gt;
          &lt;dcat:keyword xml:lang="ca"&gt;Keyword1&lt;/dcat:keyword&gt;
          &lt;dcat:distribution&gt;
              &lt;dcat:Download&gt;
                  &lt;dcat:accessURL&gt;http:/url/&lt;/dcat:accessURL&gt;
                  &lt;dct:format&gt;
                      &lt;dct:IMT&gt;
                          &lt;rdf:value&gt;application/pdf&lt;/rdf:value&gt;
                          &lt;rdfs:label&gt;pdf&lt;/rdfs:label&gt;
                      &lt;/dct:IMT&gt;
                  &lt;/dct:format&gt;
                  &lt;dct:modified rdf:datatype="http://www.w3.or/2001/XMLSchema#date"&gt;2012-11-09T16:23:22&lt;/dct:modified&gt;
             &lt;/dcat:Download&gt;
          &lt;/dcat:distribution&gt;
          &lt;dct:publisher&gt;
             &lt;foaf:Organization&gt;
                &lt;dct:title xml:lang="en"&gt;Company&lt;/dct:title&gt;
                &lt;foaf:homepage rdf:resource="http://url/"/&gt;
             &lt;/foaf:Organization&gt;
          &lt;/dct:publisher&gt;
      &lt;/dcat:Dataset&gt;
  &lt;/dcat:dataset&gt;
  </code></pre>
  
  <p>I'm so far to get every statement, which is directly beneath dcat:Dataset (<a href="https://stackoverflow.com/questions/16939159/iterate-over-specific-resource-in-rdf-file-with-jena">Iterate over specific resource in RDF file with Jena</a>), but I want to find every triple in every level. My output should look like this: </p>
  
  <pre><code>description: Description
  license: http://creativecommons.org/licenses/by/3.0/
  keyword: Keyword1
  distribution -&gt; Download -&gt; accessurl: http:/url/
  distribution -&gt; Download -&gt; format -&gt; IMT -&gt; value: application/pdf
  distribution -&gt; Download -&gt; format -&gt; IMT -&gt; label: pdf
  ...
  </code></pre>
  
  <p>I've tried it with a recursive function, which iterates over the statements and when a statement is not a literal it follows the object to the next node. Like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>private String recursiveQuery(Statement stmt) {
      Resource subject = stmt.getSubject();
      Property predicate = stmt.getPredicate();
      RDFNode object = stmt.getObject();
  
      if(object.isLiteral()) {
          out.println("LIT: " + predicate.getLocalName());
          return object.toString();
  
      } else {
          out.println(predicate.getLocalName());
          Resource r = stmt.getResource();
          StmtIterator stmts = r.listProperties();
          while (stmts.hasNext()) {
              Statement s = stmts.next();
              out.println(s.getPredicate().getLocalName());
              return recursiveQuery(s);
          }
      }
      return null;
  
  }
  </code></pre>
  
  <p>But somehow I'm getting nowhere with this method.
  Thank you very much for every insight.</p>