如何在jena中处理文字?

时间:2014-11-13 12:32:33

标签: java rdf jena

我有两种格式的rdf。使用我开发的代码只处理一种类型的RDF。在一个rdf我只有类和另一个rdf我有类文字。如果我更改了我的代码,我收到以下错误消息:

  

线程中的异常" main" com.hp.hpl.jena.rdf.model.LiteralRequiredException:http://www.w3.org/2002/07/owl#Thing

为了更好地了解我提供两种rdf格式和我的代码

一种rdf格式是:

<?xml version="1.0" encoding="windows-1252"?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:ontology="http://earthquake.linkeddata.it/ontology/"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://earthquake.linkeddata.it/resource/Tunedmassdamper">
    <ontology:desciption rdf:datatype="http://www.w3.org/2001/XMLSchema#string">A damper mounted in structures to reduce the amplitude of mechanical vibrations</ontology:desciption>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://earthquake.linkeddata.it/resource/Localco-investigator">
    <rdfs:subClassOf rdf:resource="http://earthquake.linkeddata.it/resource/Projectperson"/>
    <ontology:desciption rdf:datatype="http://www.w3.org/2001/XMLSchema#string">A person who collaborate and communictaion with principal investigator  on study proposal, design and implementation.</ontology:desciption>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://earthquake.linkeddata.it/resource/Pressuresensor">
    <ontology:desciption rdf:datatype="http://www.w3.org/2001/XMLSchema#string">A sensor which measures pressures, typically gasses or liquids.</ontology:desciption>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>

其他rdf格式是:

<?xml version="1.0" encoding="windows-1252"?>
<rdf:RDF
    xmlns:georesource="http://www.territorio.provincia.tn.it/geodati/resource/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:geontology="http://www.territorio.provincia.tn.it/geodati/ontology/"
    xmlns:dcmibox="http://dublincore.org/documents/dcmi-box/"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" > 
  <rdf:Description rdf:about="http://www.territorio.provincia.tn.it/geodati/resource/corsi_d_acqua_naturali_e_artificiali/6226">
    <geontology:length rdf:datatype="http://www.w3.org/2001/XMLSchema#double">23.0947403412</geontology:length>
    <rdfs:label xml:lang="it">I cunet. dx del i ramo dx</rdfs:label>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <rdf:type rdf:resource="http://www.territorio.provincia.tn.it/geodati/resource/corsi_d_acqua_naturali_e_artificiali"/>
    <rdfs:label xml:lang="it">6226</rdfs:label>
    <geo:geometry rdf:resource="http://www.territorio.provincia.tn.it/geodati/resource/corsi_d_acqua_naturali_e_artificiali_6226"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.territorio.provincia.tn.it/geodati/resource/corsi_d_acqua_naturali_e_artificiali/1650">
    <geontology:length rdf:datatype="http://www.w3.org/2001/XMLSchema#double">167.800339122</geontology:length>
    <rdfs:label xml:lang="it">Rio fontane nere</rdfs:label>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <rdf:type rdf:resource="http://www.territorio.provincia.tn.it/geodati/resource/corsi_d_acqua_naturali_e_artificiali"/>
    <rdfs:label xml:lang="it">1650</rdfs:label>
    <geo:geometry rdf:resource="http://www.territorio.provincia.tn.it/geodati/resource/corsi_d_acqua_naturali_e_artificiali_1650"/>
  </rdf:Description>

</rdf:RDF>

我开发的代码如下:

for (StmtIterator iter = model.listStatements(null, RDFS.label,
                (RDFNode) null); iter.hasNext();) {
            Statement stmt = (Statement) iter.next();
            Resource subject = stmt.getSubject();
            String label = stmt.getLiteral().getString();

}

如果我改变我的代码如bellow

for (StmtIterator iter = model.listStatements(); iter.hasNext();) {
            Statement stmt = (Statement) iter.next();
            Resource subject = stmt.getSubject();
            String label = stmt.getLiteral().getString();
}

程序终止于rdf的第1部分和rdf的第2部分我收到了上述错误消息。

有没有人可以帮我管理两个使用相同代码的rdf文件?

1 个答案:

答案 0 :(得分:4)

有一些遗漏的细节“......猫头鹰#Thing”不在您的数据中。

stmt.getLiteral().getString()获取语句的对象(参见javadoc),它可能是文字,但也可以是代码找到的URI。

获取stmt.getObject()对象并使用.isResource() / .isURIResource().isAnon().isLiteral()测试其内容。

另请注意,您有语言标记的文字。

getLexicalForm优于getString