本文介绍了使用Jena在RDF/XML中编写嵌套的rdf:Description元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个这样的文件:

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>

特别是,我想知道如何用Java代码(使用Jena库)编写以下部分:

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>

我已经阅读了文献,<rdf:Description rdf:about="http://stackoverflow.com/q/24084473/1281433/Piemonte">对象(或资源?)是一个空白节点,但是我不知道如何编写它.我尝试使用以下代码:

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;
}

但我有一个例外:

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)

我在做什么错了?

推荐答案

在您叫出的部分中:

<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" .

以下是可重现此模型的Java代码:

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 );
    }
}

输出:

<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>

这篇关于使用Jena在RDF/XML中编写嵌套的rdf:Description元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 01:01