Search code examples
javardfjenasemantic-webrdfs

Writing nested rdf:Description elements in RDF/XML with Jena


I would like to have a file like this:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://stackoverflow.com/q/24084473/1281433/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:j.0="http://stackoverflow.com/q/24084473/1281433/relations/">
  <rdf:Property rdf:about="http://stackoverflow.com/q/24084473/1281433/Regione">
    <rdfs:label>Regione</rdfs:label>
  </rdf:Property>
  <rdf:Property rdf:about="http://stackoverflow.com/q/24084473/1281433/Nome">
    <rdfs:label>Nome</rdfs:label>
  </rdf:Property>
  <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/soggetto">
    <j.0:Nome>Vercelli</j.0:Nome>
    <j.0:Regione>
      <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Piemonte">
        <rdfs:label>Piemonte</rdfs:label>
      </rdf:Description>
    </j.0:Regione>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Vercelli">
    <rdfs:label>Vercelli</rdfs:label>
  </rdf:Description>
</rdf:RDF>

In particular, I would like to know how I can write, in Java code (using Jena library) the follow section:

<rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/soggetto">
    <j.0:Nome>Vercelli</j.0:Nome>
    <j.0:Regione>
      <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Piemonte">
        <rdfs:label>Piemonte</rdfs:label>
      </rdf:Description>
    </j.0:Regione>
  </rdf:Description>

I have read in the literature, the <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Piemonte"> object (or resource?) is a blank node, but I don't know how to write it. I tried to use the follow code:

public Model BlankNodeCode (Model model){
  Model model1 = model;
  StmtIterator stmt = model.listStatements();
  while (stmt.hasNext()){
    Statement statement = stmt.next();
    Resource subject = statement.getSubject();
    Property pred = statement.getPredicate();
    RDFNode object = statement.getObject();
    Object res = null;
    @SuppressWarnings("unchecked")
    Resource resource = object.as((Class<Resource>) res);
    model1.createResource(subject).addProperty(pred,model1.createResource(resource));
  }
  return model1;
}

But I get an exception:

Exception in thread "main" java.lang.NullPointerException
    at com.hp.hpl.jena.enhanced.Polymorphic.findExistingView(Polymorphic.java:75)
    at com.hp.hpl.jena.enhanced.Polymorphic.asInternal(Polymorphic.java:61)
    at com.hp.hpl.jena.enhanced.EnhNode.as(EnhNode.java:107)

What am I doing wrong here?


Solution

  • In the section that you call out:

    <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/soggetto">
        <j.0:Nome>Vercelli</j.0:Nome>
        <j.0:Regione>
          <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Piemonte">
            <rdfs:label>Piemonte</rdfs:label>
          </rdf:Description>
        </j.0:Regione>
      </rdf:Description>
    

    There's no blank node. There are just three triples:

    <http://.../soggetto>   j.0:Nome      "Vercelli" .
    <http://.../soggetto>   j.0:Regione   <http://.../Piemonte> .
    <http://.../Piemonte>   rdfs:label    "Piemonte" .
    

    Here's Java code that reproduces this model:

    import org.apache.jena.riot.Lang;
    import org.apache.jena.riot.RDFDataMgr;
    
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.Property;
    import com.hp.hpl.jena.rdf.model.Resource;
    import com.hp.hpl.jena.vocabulary.RDFS;
    
    
    public class RegioneExample {
        public static void main(String[] args) {
            String ns = "http://stackoverflow.com/q/24084473/1281433/";
    
            Model model = ModelFactory.createDefaultModel();
    
            Resource soggetto = model.createResource( ns+"soggetto" );
            Resource piemonte = model.createResource( ns+"Piemonte" );
    
            Property nome = model.createProperty( ns+"Nome" );
            Property regione = model.createProperty( ns+"Regione" );
    
            soggetto.addProperty( nome, "Vercelli" );
            soggetto.addProperty( regione, piemonte );
            piemonte.addProperty( RDFS.label, "Piemonte" );
    
            RDFDataMgr.write( System.out, model, Lang.RDFXML );
        }
    }
    

    Output:

    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:j.0="http://stackoverflow.com/q/24084473/1281433/"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
      <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/soggetto">
        <j.0:Regione>
          <rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Piemonte">
            <rdfs:label>Piemonte</rdfs:label>
          </rdf:Description>
        </j.0:Regione>
        <j.0:Nome>Vercelli</j.0:Nome>
      </rdf:Description>
    </rdf:RDF>