使用 Jena 的 RDF/XML

发布于 2024-11-23 16:15:04 字数 418 浏览 9 评论 0原文

如何使用 Jena 生成以下 RDF/XML?

<rdfs:Class rdf:about="http://example.com/A#B">
    <rdfs:subClassOf>
            <rdfs:Class rdf:about="http://example.com/A" />
     </rdfs:subClassOf>
        <rdf:Property rdf:about="http://example.com/C">
            <rdfs:range rdf:resource="http://example.com/A" />
        </rdf:Property>
</rdfs:Class>   

How can I generate the following RDF/XML using Jena?

<rdfs:Class rdf:about="http://example.com/A#B">
    <rdfs:subClassOf>
            <rdfs:Class rdf:about="http://example.com/A" />
     </rdfs:subClassOf>
        <rdf:Property rdf:about="http://example.com/C">
            <rdfs:range rdf:resource="http://example.com/A" />
        </rdf:Property>
</rdfs:Class>   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2024-11-30 16:15:05

网上有很多 Jena 教程。但是,您的要求非常简单。这是一种解决方案:

package example;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;

class RdfXmlExample {
    public static void main( String[] args ) {
        new RdfXmlExample().run();
    }

    public void run() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
        String NS = "http://example.com/test#";

        OntClass a = m.createClass( NS + "A" );
        OntClass b = m.createClass( NS + "B" );

        a.addSubClass( b );

        OntProperty c = m.createOntProperty( NS + "c" );
        c.addRange( a );

        m.write( System.out, "RDF/XML-ABBREV" );
    }
}

它会产生:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    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#">
  <rdfs:Class rdf:about="http://example.com/test#B">
    <rdfs:subClassOf>
      <rdfs:Class rdf:about="http://example.com/test#A"/>
    </rdfs:subClassOf>
  </rdfs:Class>
  <rdf:Property rdf:about="http://example.com/test#c">
    <rdfs:range rdf:resource="http://example.com/test#A"/>
  </rdf:Property>
</rdf:RDF>

There are many Jena tutorials on the web. However, what you are asking for is pretty straightforward. Here's one solution:

package example;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;

class RdfXmlExample {
    public static void main( String[] args ) {
        new RdfXmlExample().run();
    }

    public void run() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
        String NS = "http://example.com/test#";

        OntClass a = m.createClass( NS + "A" );
        OntClass b = m.createClass( NS + "B" );

        a.addSubClass( b );

        OntProperty c = m.createOntProperty( NS + "c" );
        c.addRange( a );

        m.write( System.out, "RDF/XML-ABBREV" );
    }
}

which produces:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    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#">
  <rdfs:Class rdf:about="http://example.com/test#B">
    <rdfs:subClassOf>
      <rdfs:Class rdf:about="http://example.com/test#A"/>
    </rdfs:subClassOf>
  </rdfs:Class>
  <rdf:Property rdf:about="http://example.com/test#c">
    <rdfs:range rdf:resource="http://example.com/test#A"/>
  </rdf:Property>
</rdf:RDF>
枫林﹌晚霞¤ 2024-11-30 16:15:05

@Ian Dickinson 的回答很正确。
如果您希望将此输出写入文件,您可以使用此行,然后

 m.write( new FileWriter("some-file.owl"), "RDF/XML-ABBREV" );

您可以通过 protege 或在 WebVowl 上查看此文件。

@Ian Dickinson's answer is spot on.
If you wish to write this output to a file you can use this line instead

 m.write( new FileWriter("some-file.owl"), "RDF/XML-ABBREV" );

You can then view this file either through protege or on WebVowl.

与酒说心事 2024-11-30 16:15:05

的代码+解释

  • Jena RDF API 教程
  • SPARQL 教程
  • Ontology API 概述

http://incubator.apache. org/jena/getting_started/index.html

是开始 jena 的好地方。

Code + explanation of

  • Jena RDF API tutorial
  • SPARQL tutorial
  • Ontology API overview

http://incubator.apache.org/jena/getting_started/index.html

decent place to start with jena.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文