[英]Jena Java Api to read RDF file?
I have a rdf file which has format: 我有一个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>
I want to store the values of info:has_text_value and the corresponding info:contains. 我想存储info:has_text_value的值和相应的info:contains。 I have tried a lot of ways with JENA API but have not been successful.
我已经使用JENA API尝试了很多方法,但是没有成功。 Could you please guide how i can do that.
你能指导我怎么做吗。 Any source code would be of great help.
任何源代码都会有很大的帮助。 Thanks
谢谢
If this is a representative sample of your RDF, there are a couple of problems with it: 如果这是您的RDF的代表性示例,则存在一些问题:
You should not assert that all of the prefixes are xyz
. 您不应断言所有前缀都是
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. 当使用缩写名称(例如
info:contains
或geo:something
,用于标识资源的实际URI是名称空间URI和本地名称的串联 。 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. 正确使用的名称空间URI可以消除在其他情况下类似命名的概念的歧义,例如,
computers:monitor
和reptiles:monitor
可能分别旨在代表显示屏和蜥蜴。 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. 但是,如果
computers
和reptiles
命名空间都具有相同的值,则两个URI都表示相同的资源,并且对一个资源所做的每个语句也对另一个资源进行了声明。 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. 您的示例不完整,因为未定义
info
名称空间,因此info:contains
不表示合法属性URI。
The resource title2
has a relative URI , ie what it denotes is relative to the base URI of the document. 资源
title2
具有相对URI ,即它表示的是相对于文档的基本URI而言的。 This means that, for example, if you read the file containing the document from a different location (eg on disk or from an http:
URL), the identity of title2
will change . 这意味着,例如,如果您从其他位置(例如在磁盘上或从
http:
URL)读取包含文档的文件,则title2
的标识将更改 。 You can mitigate this effect by asserting the base URI of the document by adding an xml:base
statement. 您可以通过添加
xml:base
语句来声明文档的基本URI,从而减轻这种影响。
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. 有很多在线资源向您展示如何在Jena中读取和操作RDF数据。 To get you started, here is one way of doing so:
为了让您入门,这是一种方法:
First create a Model
and load your data into. 首先创建一个
Model
并将数据加载到其中。 I'll assume that your data is in the file ./rdf/test.rdf
: 我假设您的数据在文件
./rdf/test.rdf
:
Model m = FileManager.get().loadModel( "./rdf/test.rdf" );
Now create a resource denoting title2
: 现在创建一个表示
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: 或者,创建一个属性对象以访问
info:contains
属性:
Property contains = m.getProperty( NS + "contains" );
System.out.println( "title2.contains = " + title2.getProperty( contains )
.getObject();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.