Asp1de
Asp1de

Reputation: 151

RDF/XML Jena getValue

i need a help for getting some information out of the RDF with Jena Framework. I have an RDF content like this:

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ts="http://www.test.com/testModel.owl#">  
<ts:Entity rdf:ID="1234_test"> 
 <....>
</ts>
</rdf:RDF>

Now my idea is to get out the ID from ts:Entity. This is my code:

Model model = ModelFactory.createDefaultModel();
InputStream requestBody = new ByteArrayInputStream(request.getBytes());
String BASE = "http://www.test.com/testModel.owl#";
model.read(requestBody,BASE);
requestBody.close();

StmtIterator iter = model.listStatements();
 while (iter.hasNext()) {

        Statement stmt      = iter.nextStatement();  // get next statement
        Resource  subject   = stmt.getSubject();     // get the subject
        Property  predicate = stmt.getPredicate();   // get the predicate
        RDFNode   object    = stmt.getObject();      // get the object

            System.out.println(subject + " | "+predicate);

     }

The only problem, in that case, is that i have to scroll all the Statement. There is a way for getting direct the ID from ts:Entity ? Maybe before getting the resource and after the value of the ID related to that resource.

Thanks in advance for helping.


Sorry, i am again here, because i have a similar question, if for example i have this rdf:

<rdf:RDF
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:ts="http://www.test.com/testModel.owl#">  
<ts:Entity rdf:ID="1234_test"> 
   <ts:Resource> 
       <ts:testProp rdf:datatype="http://www.w3.org/2001/XMLSchema#string">test_ID_test</ts:testProp>
 </ts>
</ts>
</rdf:RDF>

How i can extract the value test_ID_test??? And if i want use SPARQL how can i do with jena???

Upvotes: 2

Views: 1491

Answers (2)

user205512
user205512

Reputation: 8898

How about:

Resource ENTITY_TYPE = model.getResource("http://www.test.com/testModel.owl#Entity");
StmtIterator iter = model.listStatements(null, RDF.type, ENTITY_TYPE);
while (iter.hasNext()) {
    String entityID = iter.next().getSubject().getURI();
    System.out.println(entityID);
}

That will get the URI of each entity.

Upvotes: 2

Michael
Michael

Reputation: 4886

You should be using SPARQL to query your model rather than iterating over all the statements. Jena provides a good tutorial on how to use SPARQL with their API.

Upvotes: 3

Related Questions