首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache读取RDF:foaf文件

使用Apache读取RDF:foaf文件
EN

Stack Overflow用户
提问于 2015-03-20 12:26:57
回答 1查看 1.7K关注 0票数 2

我在读取RDF文件时遇到了问题,RDF文件使用foaf标记。我想和阿帕奇·耶娜一起读。下面是RDF文件的片段。

代码语言:javascript
复制
<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>

我只是不明白如何在普通POJO对象中使用Apache读取这些数据。任何帮助都将不胜感激(在网上找不到此类解析的教程)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-26 00:09:05

我不知道我是否理解你的问题。但是,如果需要将RDF文件读入POJO对象,则有很多选择。例如,可以使用Jena将rdf文件读取到模型中,然后使用框架建议的方法创建POJO对象,以获取属性的值。这是一个从文件中提取foaf:firstName的代码示例。

代码语言:javascript
复制
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);

    }
}

您可以在POJO类的setter中使用这些方法。你可以找到一个很好的介绍这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29166488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档