当前位置: 首页 > 知识库问答 >
问题:

用Jena解析RDF递归

梁丘霖
2023-03-14
<dcat:dataset>
    <dcat:Dataset rdf:about="http://url/" > 
        <dct:description xml:lang="ca">Description</dct:description>
        <dct:license rdf:resource="http://creativecommons.org/licenses/by/3.0/"/>
        <dcat:keyword xml:lang="ca">Keyword1</dcat:keyword>
        <dcat:distribution>
            <dcat:Download>
                <dcat:accessURL>http:/url/</dcat:accessURL>
                <dct:format>
                    <dct:IMT>
                        <rdf:value>application/pdf</rdf:value>
                        <rdfs:label>pdf</rdfs:label>
                    </dct:IMT>
                </dct:format>
                <dct:modified rdf:datatype="http://www.w3.or/2001/XMLSchema#date">2012-11-09T16:23:22</dct:modified>
           </dcat:Download>
        </dcat:distribution>
        <dct:publisher>
           <foaf:Organization>
              <dct:title xml:lang="en">Company</dct:title>
              <foaf:homepage rdf:resource="http://url/"/>
           </foaf:Organization>
        </dct:publisher>
    </dcat:Dataset>
</dcat:dataset>
description: Description
license: http://creativecommons.org/licenses/by/3.0/
keyword: Keyword1
distribution -> Download -> accessurl: http:/url/
distribution -> Download -> format -> IMT -> value: application/pdf
distribution -> Download -> format -> IMT -> label: pdf
...

我用一个递归函数尝试过它,它迭代语句,当语句不是文字时,它会跟随对象到达下一个节点。像这样:

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;

}

但不知怎的,我用这个方法没有什么进展。非常感谢你的每一次洞察。

共有1个答案

拓拔谭三
2023-03-14
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dcat="http://www.w3.org/ns/dcat#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:dct="http://purl.org/dc/terms/"
    xmlns:dctypes="http://purl.org/dc/dcmitype/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <dcat:Catalog rdf:about="http://uri/">
    <dcat:dataset>
    <dcat:Dataset rdf:about="http://url/" > 
        <dct:description xml:lang="ca">Description</dct:description>
        <dct:license rdf:resource="http://creativecommons.org/licenses/by/3.0/"/>
        <dcat:keyword xml:lang="ca">Keyword1</dcat:keyword>
        <dcat:distribution>
            <dcat:Download>
                <dcat:accessURL>http:/url/</dcat:accessURL>
                <dct:format>
                    <dct:IMT>
                        <rdf:value>application/pdf</rdf:value>
                        <rdfs:label>pdf</rdfs:label>
                    </dct:IMT>
                </dct:format>
                <dct:modified rdf:datatype="http://www.w3.or/2001/XMLSchema#date">2012-11-09T16:23:22</dct:modified>
           </dcat:Download>
        </dcat:distribution>
        <dct:publisher>
           <foaf:Organization>
              <dct:title xml:lang="en">Company</dct:title>
              <foaf:homepage rdf:resource="http://url/"/>
           </foaf:Organization>
        </dct:publisher>
    </dcat:Dataset>
    </dcat:dataset>
  </dcat:Catalog>
 </rdf:RDF>
import java.util.HashSet;
import java.util.Set;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;


public class DFSinRDFwithJena {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        model.read( "rdfdfs.rdf" );

        StmtIterator stmts = model.listStatements( null, RDF.type, model.getResource( "http://www.w3.org/ns/dcat#" + "Dataset" ));
        while ( stmts.hasNext() ) {
            rdfDFS( stmts.next().getSubject(), new HashSet<RDFNode>(), "" );
        }
        model.write( System.out, "N3" );
    }

    public static void rdfDFS( RDFNode node, Set<RDFNode> visited, String prefix ) {
        if ( visited.contains( node )) {
            return;
        }
        else {
            visited.add( node );
            System.out.println( prefix + node );
            if ( node.isResource() ) {
                StmtIterator stmts = node.asResource().listProperties();
                while ( stmts.hasNext() ) {
                    Statement stmt = stmts.next();
                    rdfDFS( stmt.getObject(), visited, prefix + node + " =[" + stmt.getPredicate() + "]=> " );
                }
            }
        }
    }
}
http://url/
http://url/ =[http://purl.org/dc/terms/publisher]=> -f6d9b42:13f2e8dc5fb:-7ffd
http://url/ =[http://purl.org/dc/terms/publisher]=> -f6d9b42:13f2e8dc5fb:-7ffd =[http://purl.org/dc/terms/title]=> Company@en
http://url/ =[http://purl.org/dc/terms/publisher]=> -f6d9b42:13f2e8dc5fb:-7ffd =[http://www.w3.org/1999/02/22-rdf-syntax-ns#type]=> http://xmlns.com/foaf/0.1/Organization
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://purl.org/dc/terms/modified]=> 2012-11-09T16:23:22^^http://www.w3.or/2001/XMLSchema#date
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://purl.org/dc/terms/format]=> -f6d9b42:13f2e8dc5fb:-7ffe
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://purl.org/dc/terms/format]=> -f6d9b42:13f2e8dc5fb:-7ffe =[http://www.w3.org/2000/01/rdf-schema#label]=> pdf
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://purl.org/dc/terms/format]=> -f6d9b42:13f2e8dc5fb:-7ffe =[http://www.w3.org/1999/02/22-rdf-syntax-ns#value]=> application/pdf
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://purl.org/dc/terms/format]=> -f6d9b42:13f2e8dc5fb:-7ffe =[http://www.w3.org/1999/02/22-rdf-syntax-ns#type]=> http://purl.org/dc/terms/IMT
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://www.w3.org/ns/dcat#accessURL]=> http:/url/
http://url/ =[http://www.w3.org/ns/dcat#distribution]=> -f6d9b42:13f2e8dc5fb:-7fff =[http://www.w3.org/1999/02/22-rdf-syntax-ns#type]=> http://www.w3.org/ns/dcat#Download
http://url/ =[http://www.w3.org/ns/dcat#keyword]=> Keyword1@ca
http://url/ =[http://purl.org/dc/terms/license]=> http://creativecommons.org/licenses/by/3.0/
http://url/ =[http://purl.org/dc/terms/description]=> Description@ca
http://url/ =[http://www.w3.org/1999/02/22-rdf-syntax-ns#type]=> http://www.w3.org/ns/dcat#Dataset
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dcat="http://www.w3.org/ns/dcat#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:dct="http://purl.org/dc/terms/"
    xmlns:dctypes="http://purl.org/dc/dcmitype/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:nodeID="A0">
    <dct:modified rdf:datatype="http://www.w3.or/2001/XMLSchema#date">2012-11-09T16:23:22</dct:modified>
    <dct:format rdf:nodeID="A1"/>
    <dcat:accessURL>http:/url/</dcat:accessURL>
    <rdf:type rdf:resource="http://www.w3.org/ns/dcat#Download"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://uri/">
    <dcat:dataset rdf:resource="http://url/"/>
    <rdf:type rdf:resource="http://www.w3.org/ns/dcat#Catalog"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://url/">
    <dct:publisher rdf:nodeID="A2"/>
    <dcat:distribution rdf:nodeID="A0"/>
    <dcat:keyword xml:lang="ca">Keyword1</dcat:keyword>
    <dct:license rdf:resource="http://creativecommons.org/licenses/by/3.0/"/>
    <dct:description xml:lang="ca">Description</dct:description>
    <rdf:type rdf:resource="http://www.w3.org/ns/dcat#Dataset"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A2">
    <foaf:homepage rdf:resource="http://url/"/>
    <dct:title xml:lang="en">Company</dct:title>
    <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Organization"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A1">
    <rdfs:label>pdf</rdfs:label>
    <rdf:value>application/pdf</rdf:value>
    <rdf:type rdf:resource="http://purl.org/dc/terms/IMT"/>
  </rdf:Description>
</rdf:RDF>
 类似资料:
  • 我需要帮助从RDF和Jena框架中获取一些信息。我有一个RDF内容是这样的: 我如何提取值test_id_test???如果我想用SPARQL,我怎么用Jena???

  • 我想把xml文件转换成RDF。我认为JENA GRDDL可以使用,但我不知道它是如何工作的!我没有找到任何例子!你能帮助我请开始使用这个API或显示我任何可能的解决方案!

  • 我正在尝试使用Jena解析Freebase-RDF-2014-01-12-00-00.gz(25 GB)的Freebase-RDF-2014-01-12-00-00.gz。耶拿报告了许多关于不良数据的问题。示例-150.0无效,true和false值无效我通过在decimal和true/false的转储文件中添加双引号来解决这些问题。但是,Jena仍在 有没有办法对这个数据进行预处理,这样我就不用

  • 下面是java代码: 这有帮助吗?我真的不知道了...