我有一个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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
我想存储info:has_text_value的值和相应的info:contains.我已经尝试了很多JENA API的方法,但没有成功.你能指导我如何做到这一点.任何源代码都会有很大的帮助.谢谢
如果这是您的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 将更改.您可以通过添加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>
Run Code Online (Sandbox Code Playgroud)
有很多在线资源可以向您展示如何在Jena中读取和操作RDF数据.为了帮助您入门,这是一种方法:
首先创建Model并加载您的数据.我假设你的数据在文件中./rdf/test.rdf:
Model m = FileManager.get().loadModel( "./rdf/test.rdf" );
Run Code Online (Sandbox Code Playgroud)
现在创建一个表示的资源title2:
String NS = "http://example.org/data/test#";
Resource title2 = m.getResource( NS + "title2" );
Run Code Online (Sandbox Code Playgroud)
现在列出资源的属性:
for (StmtIterator i = title2.listProperties(); i.hasNext(); ) {
Statement s = i.next();
System.out.println( "title2 has property " + s.getPredicate() +
" with value " + s.getObject() );
}
Run Code Online (Sandbox Code Playgroud)
或者,创建一个属性对象来访问该info:contains属性:
Property contains = m.getProperty( NS + "contains" );
System.out.println( "title2.contains = " + title2.getProperty( contains )
.getObject();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7796 次 |
| 最近记录: |