我在读取RDF文件时遇到了问题,RDF文件使用foaf标记。我想和阿帕奇·耶娜一起读。下面是RDF文件的片段。
<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读取这些数据。任何帮助都将不胜感激(在网上找不到此类解析的教程)。
发布于 2015-03-26 00:09:05
我不知道我是否理解你的问题。但是,如果需要将RDF文件读入POJO对象,则有很多选择。例如,可以使用Jena将rdf文件读取到模型中,然后使用框架建议的方法创建POJO对象,以获取属性的值。这是一个从文件中提取foaf:firstName的代码示例。
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中使用这些方法。你可以找到一个很好的介绍这里
https://stackoverflow.com/questions/29166488
复制相似问题