开发者

Jena Java Api to read RDF file?

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>
  <开发者_如何学Go/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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜