Jena Java Api 读取 RDF 文件?

发布于 2024-11-16 22:15:51 字数 781 浏览 5 评论 0原文

我有一个 rdf 文件,其格式为:

<rdf:RDF
xmlns:geo="xyz"
xmlns:quality="xyz"
xmlns:purl="xyz"
xmlns:swrlb="xyz"> 

  <rdf:Description rdf:about="title1">
<rdf:type rdf:resource="resource22"/>
<info:contains>fromdisk1</info:contains>
<info:has_text_value>
The location 
</info:has_text_value>
  </rdf:Description>

<rdf:Description rdf:about="title2">
<rdf:type rdf:resource="resource12"/>
<info:contains>fromdisk2</info:contains>
<info:has_text_value>
xyz 
   </info:has_text_value>
  </rdf:Description>

  </rdf:RDF>

我想存储 info:has_text_value 的值和相应的 info:contains。我已经尝试了很多使用 JENA API 的方法,但都没有成功。您能指导我如何做到这一点吗?任何源代码都会有很大帮助。谢谢

I have a rdf file which has format:

<rdf:RDF
xmlns:geo="xyz"
xmlns:quality="xyz"
xmlns:purl="xyz"
xmlns:swrlb="xyz"> 

  <rdf:Description rdf:about="title1">
<rdf:type rdf:resource="resource22"/>
<info:contains>fromdisk1</info:contains>
<info:has_text_value>
The location 
</info:has_text_value>
  </rdf:Description>

<rdf:Description rdf:about="title2">
<rdf:type rdf:resource="resource12"/>
<info:contains>fromdisk2</info:contains>
<info:has_text_value>
xyz 
   </info:has_text_value>
  </rdf:Description>

  </rdf:RDF>

I want to store the values of info:has_text_value and the corresponding info:contains. I have tried a lot of ways with JENA API but have not been successful. Could you please guide how i can do that. Any source code would be of great help. Thanks

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

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

发布评论

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

评论(1

喵星人汪星人 2024-11-23 22:15:52

如果这是 RDF 的代表性示例,则存在一些问题:

  • 不应该断言所有前缀都是xyz。当您使用缩写名称(例如 info:containsgeo:something)时,用于标识资源的实际 URI 是串联命名空间 URI 和本地名称。如果使用得当,命名空间 URI 可以消除类似命名概念的歧义,例如,computers:monitorreptiles:monitor 可能旨在表示显示屏和蜥蜴,分别。但是,如果 computersreptiles 命名空间具有相同的值,则两个 URI 都表示相同的资源,并且关于一个资源的每个语句也都是关于另一个资源的。这不是一个好主意。

  • 您的示例不完整,因为未定义 info 命名空间,因此 info:contains 不表示合法的属性 URI。

  • 资源title2有一个相对URI,即它表示的是相对于文档的基本URI。这意味着,例如,如果您从不同位置(例如在磁盘上或从 http: URL)读取包含文档的文件,则 title2 的标识 < em>将会改变。您可以通过添加 xml:base 语句来断言文档的基本 URI,从而减轻这种影响。

解决这些问题(并对您的名称空间进行猜测),可以得到:

<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:geo="http://example.org/schema/geo#"
  xmlns:quality="http://example.org/schema/quality#"
  xmlns:purl="http://example.org/schema/purl#"
  xmlns:swrlb="http://example.org/schema/swrlb#"
  xmlns:info="http://example.org/schema/info#"
  xml:base="http://example.org/data/test#"
> 

<rdf:Description rdf:about="title1">
  <rdf:type rdf:resource="resource22"/>
  <info:contains>fromdisk1</info:contains>
  <info:has_text_value>The location</info:has_text_value>
</rdf:Description>

<rdf:Description rdf:about="title2">
  <rdf:type rdf:resource="resource12"/>
  <info:contains>fromdisk2</info:contains>
  <info:has_text_value>xyz</info:has_text_value>
</rdf:Description>

</rdf:RDF>

有很多在线资源可以向您展示如何在 Jena 中读取和操作 RDF 数据。为了帮助您开始,这里有一种方法:

首先创建一个模型并将数据加载到其中。我假设您的数据位于文件 ./rdf/test.rdf 中:

Model m = FileManager.get().loadModel( "./rdf/test.rdf" );

现在创建一个表示 title2 的资源:

String NS = "http://example.org/data/test#";
Resource title2 = m.getResource( NS + "title2" );

现在列出该资源的属性:

for (StmtIterator i = title2.listProperties(); i.hasNext(); ) {
    Statement s = i.next();
    System.out.println( "title2 has property " + s.getPredicate() + 
                        " with value " + s.getObject() );
}

或者,创建一个属性对象来访问 info:contains 属性:

Property contains = m.getProperty( NS + "contains" );
System.out.println( "title2.contains = " + title2.getProperty( contains )
                                                 .getObject();

If this is a representative sample of your RDF, there are a couple of problems with it:

  • You should not assert that all of the prefixes are xyz. When you use a shortened name, such as info:contains or geo:something, the actual URI being used to identify the resource is the concatenation of the namespace URI and the local name. Properly used, namespace URI's can disambiguate what would otherwise be similarly named concepts, for example computers:monitor and reptiles:monitor might be intended to represent a display screen and a lizard, respectively. However, if both computers and reptiles namespaces have the same value, then both URI's denote the same resource and every statement made about one resource is also made about the other. Not a good idea.

  • Your sample is incomplete because the info namespace is not defined, so info:contains does not denote a legal property URI.

  • The resource title2 has a relative URI, i.e. what it denotes is relative to the base URI of the document. This means that, for example, if you read the file containing the document from a different location (e.g. on disk or from an http: URL), the identity of title2 will change. You can mitigate this effect by asserting the base URI of the document by adding an xml:base statement.

Fixing these problems (and making guesses about your namespaces), gets:

<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:geo="http://example.org/schema/geo#"
  xmlns:quality="http://example.org/schema/quality#"
  xmlns:purl="http://example.org/schema/purl#"
  xmlns:swrlb="http://example.org/schema/swrlb#"
  xmlns:info="http://example.org/schema/info#"
  xml:base="http://example.org/data/test#"
> 

<rdf:Description rdf:about="title1">
  <rdf:type rdf:resource="resource22"/>
  <info:contains>fromdisk1</info:contains>
  <info:has_text_value>The location</info:has_text_value>
</rdf:Description>

<rdf:Description rdf:about="title2">
  <rdf:type rdf:resource="resource12"/>
  <info:contains>fromdisk2</info:contains>
  <info:has_text_value>xyz</info:has_text_value>
</rdf:Description>

</rdf:RDF>

There are lots of online resources to show you how to read and manipulate RDF data in Jena. To get you started, here is one way of doing so:

First create a Model and load your data into. I'll assume that your data is in the file ./rdf/test.rdf:

Model m = FileManager.get().loadModel( "./rdf/test.rdf" );

Now create a resource denoting title2:

String NS = "http://example.org/data/test#";
Resource title2 = m.getResource( NS + "title2" );

Now list the properties of the resource:

for (StmtIterator i = title2.listProperties(); i.hasNext(); ) {
    Statement s = i.next();
    System.out.println( "title2 has property " + s.getPredicate() + 
                        " with value " + s.getObject() );
}

Alternatively, create a property object to access the info:contains property:

Property contains = m.getProperty( NS + "contains" );
System.out.println( "title2.contains = " + title2.getProperty( contains )
                                                 .getObject();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文