java - 如何加载 N-Triples 模型并从 RDF 文件中获取 namespace ?(新手)

标签 java xml rdf jena

我有一个像这样的 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()?我应该使用其他方法吗?

最佳答案

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

关于java - 如何加载 N-Triples 模型并从 RDF 文件中获取 namespace ?(新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958270/

相关文章:

java - 如何删除列表中的前 X 条目<Double>

java - 在main中调用其他类的方法

ASP.NET 3.5 绑定(bind)到 XML 字符串

csv - 将 CSV 转换为 RDF,其中一列是一组值

rdf - 猫头鹰 rdfs :langString maxCardinality per language

java - LibGDX/Android开发: Managing cameras across multiple screens

java - Web 驱动程序的依赖问题 - 无法从最终类继承

Python Sparql 查询本地文件

java - 在 web.xml 中加载启动标记

java - 如何使用 spring IDE 查看 spring 项目的类图