jenajava-ee-7foaf

Read RDF:foaf file with Apache Jena


I have a problem with reading RDF file, which is using foaf tags. I would like to read it with Apache Jena. Below is the snippet of the RDF file.

<rdf:RDF xmlns="http://test.example.com/"     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"     xmlns:foaf="http://xmlns.com/foaf/0.1/">
<foaf:Person rdf:about="http://test.example.com/MainPerson.rdf">
<foaf:firstName>John</foaf:firstName>
<foaf:lastName>Doe</foaf:lastName>
<foaf:nick>Doe</foaf:nick>
<foaf:gender>Male</foaf:gender>
<foaf:based_near>Honolulu</foaf:based_near>
<foaf:birthday>08-14-1990</foaf:birthday>
<foaf:mbox>john@example.com</foaf:mbox>
<foaf:homepage rdf:resource="http://www.example.com"/>
<foaf:img rdf:resource="http://weknowmemes.com/wp-content/uploads/2013/09/wat-meme.jpg"/>
<foaf:made>
Article: Developing applications in Java
</foaf:made>
<foaf:age>24</foaf:age>
<foaf:interest>
Java, Java EE (web tier), PrimeFaces, MySQL, PHP, OpenCart, Joomla,   Prestashop, CSS3, HTML5
</foaf:interest>
<foaf:pastProject rdf:resource="http://www.supercombe.si"/>
<foaf:status>Student</foaf:status>
<foaf:geekcode>M+, L++</foaf:geekcode>
<foaf:knows>
<foaf:Person>
<rdfs:seeAlso rdf:resource="http://test.example.com/Person.rdf"/>
</foaf:Person>
</foaf:knows>
<foaf:knows>
<foaf:Person>
<rdfs:seeAlso rdf:resource="http://test.example.com/Person2.rdf"/>
</foaf:Person>
</foaf:knows>
<foaf:knows>
<foaf:Person>
<rdfs:seeAlso rdf:resource="http://test.example.com/Person3.rdf"/>
</foaf:Person>
</foaf:knows>
</foaf:Person>
</rdf:RDF>

I just don't understand how to read this data with Apache Jena in regular POJO object. Any help will be appreciated (couldn't find tutorial on the web for this kind of parsing).


Solution

  • I don't know if I understood your problem. But if you need to read RDF file to a POJO object, you have a lot of choice. For example, you can read your rdf file using Jena to a model, and then create POJO objects using the methods proposed by the framework to get the values of your properties. This is a code example that extracts the foaf:firstName from your file

    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.Property;
    import com.hp.hpl.jena.rdf.model.Resource;
    import com.hp.hpl.jena.util.FileManager;
    
    public class Test {
        //First, create a Jena model and use FileManager to read the file
        public static Model model = ModelFactory.createDefaultModel();
        public static void main(String[] args) {
           //Use FileManager to read the file and add it to the Jena model
            FileManager.get().readModel(model, "test.rdf");
           //Apply methods like getResource, getProperty, listStatements,listLiteralStatements ...
           //to your model to extract the information you want
            Resource person = model.getResource("http://test.example.com/MainPerson.rdf");
            Property firstName = model.createProperty("http://xmlns.com/foaf/0.1/firstName");
            String firstNameValue = person.getProperty(firstName).getString();
            System.out.println(firstNameValue);
    
        }
    }
    

    You can use those methods in the setters of your POJO class. You can find a very good introduction here