• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Infexpert

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Jena使用说明(一)

对于官方网站的部分摘要。(源:http://jena.sourceforge.net/tutorial/RDF_API/index.html)

一、介绍:

控制前缀(Controlling Prefixes):

声明前缀定义(explicit prefix definitions):

 

Model m = ModelFactory.createDefaultModel();

 String nsA = "http://somewhere/else#";

 String nsB = "http://nowhere/else#";

 Resource root = m.createResource( nsA + "root" );

 Property P = m.createProperty( nsA + "P" );

 Property Q = m.createProperty( nsB + "Q" );

 Resource x = m.createResource( nsA + "x" );

 Resource y = m.createResource( nsA + "y" );

 Resource z = m.createResource( nsA + "z" );

 m.add( root, P, x ).add( root, P, y ).add( y, Q, z );

 System.out.println( "# -- no special prefixes defined" );

 m.write( System.out );

 System.out.println( "# -- nsA defined" );

 m.setNsPrefix( "nsA", nsA );

 m.write( System.out );

 System.out.println( "# -- nsA and cat defined" );

 m.setNsPrefix( "cat", nsB );

 m.write( System.out );

 

输出:  # -- no special prefixes defined <rdf:RDF    xmlns:j.0="http://nowhere/else#"    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:j.1="http://somewhere/else#" >  <rdf:Description rdf:about="http://somewhere/else#root">    <j.1:P rdf:resource="http://somewhere/else#x"/>    <j.1:P rdf:resource="http://somewhere/else#y"/>  </rdf:Description>  <rdf:Description rdf:about="http://somewhere/else#y">    <j.0:Q rdf:resource="http://somewhere/else#z"/>  </rdf:Description></rdf:RDF> # -- nsA defined <rdf:RDF    xmlns:j.0="http://nowhere/else#"    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:nsA="http://somewhere/else#" >  <rdf:Description rdf:about="http://somewhere/else#root">    <nsA:P rdf:resource="http://somewhere/else#x"/>    <nsA:P rdf:resource="http://somewhere/else#y"/>  </rdf:Description>  <rdf:Description rdf:about="http://somewhere/else#y">    <j.0:Q rdf:resource="http://somewhere/else#z"/>  </rdf:Description></rdf:RDF>   # -- nsA and cat defined <rdf:RDF    xmlns:cat="http://nowhere/else#"    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:nsA="http://somewhere/else#" >  <rdf:Description rdf:about="http://somewhere/else#root">    <nsA:P rdf:resource="http://somewhere/else#x"/>    <nsA:P rdf:resource="http://somewhere/else#y"/>  </rdf:Description>  <rdf:Description rdf:about="http://somewhere/else#y">    <cat:Q rdf:resource="http://somewhere/else#z"/>  </rdf:Description></rdf:RDF>  

 

 

读取RDF文件: Model m2 = ModelFactory.createDefaultModel();m2.read( "file:/tmp/fragment.rdf" );m2.write( System.out );  

增加前缀:setNsPrefix

移除前缀:removeNsPrefix(String prefix)

PrefixMapping

 

 

Jena RDF包

 

Com.hp.hpl.jena.rdf.model

Com.hp.hpl.jena.tutorial

Com.hp.hpl.jena…impl  (createResource,ResourceImpl, PropertyImpl,LiteralImpl)

 

导航一个Model:

 

检索 resource  // retrieve the John Smith vcard resource from the modelResource vcard = model.getResource(johnSmithURI);  

检索property

// retrieve the value of the N property

Resource name = (Resource) vcard.getProperty(VCARD.N)

                                .getObject();

 

 

// retrieve the value of the FN property

Resource name = vcard.getProperty(VCARD.N)

                     .getResource();

 

// retrieve the given name property

String fullName = vcard.getProperty(VCARD.FN)

                        .getString();

 

一般resource只有一个 vcard:FN 和一个 vcard:N属性. RDF允许重复增加属性:增加两个昵称

// add two nickname properties to vcard

vcard.addProperty(VCARD.NICKNAME, "Smithy")

     .addProperty(VCARD.NICKNAME, "Adman");

 

一般取出多个昵称是不能取出的,例如:

  Vcard.getProperty(VCARD.NICKNAME)  是不明确的一个结果。

我们能够用如下方法列出所有的昵称:

 

// set up the output

System.out.println("The nicknames of \""

                      + fullName + "\" are:");

// list the nicknames

StmtIterator iter = vcard.listProperties(VCARD.NICKNAME);

while (iter.hasNext()) {

    System.out.println("    " + iter.nextStatement()

                                    .getObject()

                                    .toString());

}

 -----待续----

posted on 2012-01-06 08:42  Infexpert  阅读(682)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3