[英]Mapping RDF/XML data to java objects
我已經設置了一個Web爬網程序,該爬網程序收集數據並將其存儲在rdf / xml文件中,現在我想將該數據映射到我的Java對象...我該怎么做?
我發現這段代碼可能對我有用,但是我似乎無法正確使用它...它從rdf / xml文件中收集主題,謂詞和對象,但是我可以用我的java指定的對象表示該數據,我不知道該怎么...我用谷歌搜索了很多,但是對此沒有很多有用的東西,所以幫幫我!
StmtIterator iter = rdfGraph.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject();
//System.out.print(subject.getNameSpace( ) + subject.getLocalName( ));// get the subject
Property predicate = stmt.getPredicate();
//System.out.print(" " + predicate.getNameSpace( ) + predicate.getLocalName( ));// get the predicate
RDFNode object = stmt.getObject(); // get the object
//System.out.println(" " + object.toString( ) + "\n");
System.out.println(subject + " | "+predicate + " | " + object);
}
這是我的rdf文件的一部分...
<rdf:Description rdf:nodeID="A12">
<schema:reviewRating rdf:nodeID="A13"/>
<schema:description>descriptiooooon</schema:description>
<schema:datePublished>2012-02-22</schema:datePublished>
<schema:author>Nick M.</schema:author>
<rdf:type rdf:resource="http://schema.org/Review"/>
</rdf:Description>
我想用這個java對象代表它。這是我的課...
@Namespace(Constants.SCHEMA)
@RdfType("Review")
public class Review extends Thing{
@RdfProperty(Constants.SCHEMA + "author")
private String author;
@RdfProperty(Constants.SCHEMA + "reviewRating")
private Rating reviewRating;
@RdfProperty(Constants.SCHEMA + "datePublished")
private Date datePublished;
@RdfProperty(Constants.SCHEMA + "description")
private String description;
}
也許這個API對您可能有用: XMappr API
好吧,您還沒有告訴我們
我可以用我的java指定的對象表示此數據,
實際上意味着,因為您尚未描述Java對象。
我猜是一個問題,問題是您一次訪問RDF模型的內容只有一個三元組( Statement
)。 這是一種低級訪問模式。 您需要識別的第一件事是具有相同主題URI的三元組表示有關相同RDF資源的信息。 因此,您可以做的一件事是遍歷模型的主題,然后列出它們的屬性:
ResourceIterator i = rdfGraph.listSubjects();
while (i.hasNext()) {
Resource s = i.next();
System.out.println( "Graph contains subject " + s );
for (StmtIterator j = s.listProperties(); j.hasNext(); ) {
Statement t = j.next();
System.out.println( " with property " + t.getPredicate() +
" ---> " + t.getObject() );
}
}
這將使您對圖中的語句分組的基本層次有所了解。 實際上,您可能只想從某種rdf:type
資源開始,這很容易做到。
這將是使用RDF數據的更常見的基本模式。 如果確實需要將圖形中的資源映射到Java對象,請查看JenaBean之類的項目。 但是請注意,該代碼已經很舊了,可能需要進行修改才能與當前版本的Jena一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.