Jena Java Api 读取 RDF 文件?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是 RDF 的代表性示例,则存在一些问题:
您不应该断言所有前缀都是
xyz
。当您使用缩写名称(例如info:contains
或geo:something
)时,用于标识资源的实际 URI 是串联命名空间 URI 和本地名称。如果使用得当,命名空间 URI 可以消除类似命名概念的歧义,例如,computers:monitor
和reptiles:monitor
可能旨在表示显示屏和蜥蜴,分别。但是,如果computers
和reptiles
命名空间具有相同的值,则两个 URI 都表示相同的资源,并且关于一个资源的每个语句也都是关于另一个资源的。这不是一个好主意。您的示例不完整,因为未定义
info
命名空间,因此info:contains
不表示合法的属性 URI。资源
title2
有一个相对URI,即它表示的是相对于文档的基本URI。这意味着,例如,如果您从不同位置(例如在磁盘上或从http:
URL)读取包含文档的文件,则title2
的标识 < em>将会改变。您可以通过添加xml:base
语句来断言文档的基本 URI,从而减轻这种影响。解决这些问题(并对您的名称空间进行猜测),可以得到:
有很多在线资源可以向您展示如何在 Jena 中读取和操作 RDF 数据。为了帮助您开始,这里有一种方法:
首先创建一个
模型
并将数据加载到其中。我假设您的数据位于文件./rdf/test.rdf
中:现在创建一个表示
title2
的资源:现在列出该资源的属性:
或者,创建一个属性对象来访问
info:contains
属性: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 asinfo:contains
orgeo: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 examplecomputers:monitor
andreptiles:monitor
might be intended to represent a display screen and a lizard, respectively. However, if bothcomputers
andreptiles
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, soinfo: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 anhttp:
URL), the identity oftitle2
will change. You can mitigate this effect by asserting the base URI of the document by adding anxml:base
statement.Fixing these problems (and making guesses about your namespaces), gets:
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
:Now create a resource denoting
title2
:Now list the properties of the resource:
Alternatively, create a property object to access the
info:contains
property: