【问题标题】:How to load an N-Triples model and obtain the namespaces from RDF file?(newbie)如何加载 N-Triples 模型并从 RDF 文件中获取命名空间?(新手)
【发布时间】:2014-11-16 14:56:01
【问题描述】:

我有一个这样的 RDF/XML 文件:

<rdf:RDF
xmlns:go="http://www.geneontology.org/dtds/go.dtd#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
        <go:term rdf:about="http://www.geneontology.org/go#GO:0000001">
            <go:accession>GO:0000001</go:accession>
            <go:name>mitochondrion inheritance</go:name>
            <go:synonym>mitochondrial inheritance</go:synonym>
            <go:definition>The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton.</go:definition>
            <go:is_a rdf:resource="http://www.geneontology.org/go#GO:0048308" />
            <go:is_a rdf:resource="http://www.geneontology.org/go#GO:0048311" />
        </go:term>
</rdf:RDF>

然后我必须把这个 RDF/XML 转换成 N-Triples 格式,我已经用 Jena 自动完成了:

FileManager.get().addLocatorClassLoader(Converter.class.getClassLoader());
    Model model = FileManager.get().loadModel("smallfacts.rdf");
    try {

          File file= new File("outputsmall.txt");
          model.write(new FileOutputStream(file), "N-Triples");
        } catch (IOException e) {
          e.printStackTrace();

但是我需要用前缀来缩短每个谓词。我必须得到这样的东西:

<http://www.geneontology.org/go#GO:2000391> is_a <http://www.geneontology.org/go#GO:2000389>.
<http://www.geneontology.org/go#GO:2000391> accession "GO:2000391".

我读过getNSPrefix(),并做了一些尝试,比如:

FileManager.get().addLocatorClassLoader(Converter.class.getClassLoader());
    Model model = FileManager.get().loadModel("smallfacts.rdf");
    try {

          File file= new File("outputsmall.txt");
          model.getNsPrefixURI("http://www.geneontology.org/dtds/go.dtd#");
          model.write(new FileOutputStream(file), "N-Triples", "http://www.geneontology.org/dtds/go.dtd#");
        } catch (IOException e) {
          e.printStackTrace();

在这种情况下如何使用getNsPrefixURI()?我应该使用其他方法吗?

【问题讨论】:

  • n-triples 格式没有 iri 前缀。 IRI 始终是完整的。

标签: java xml rdf jena


【解决方案1】:

尝试使用 RDFDataMgr 和 RDFFormat.TURTLE_FLAT 编写您的 RDF 数据。 https://jena.apache.org/documentation/io/rdf-output.html

【讨论】:

  • 谢谢AndyS,这正是我需要的:)
  • @AriWijayanti 如果这是您需要的,请务必accept it
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2017-04-11
  • 2021-09-25
  • 1970-01-01
  • 2011-11-07
  • 2014-01-29
  • 1970-01-01
相关资源
最近更新 更多