JenaResource
有一个方法listProperties
,您可以使用该方法迭代以资源为主题并具有给定属性的语句。这是一个描述RDF Primer及其两个编辑器的示例(在此示例中称为作者,以便与您的示例保持一致)。
public class MultipleProperties {
public static void main(String[] args) {
String ns = "http://www.example.com/";
Model model = ModelFactory.createDefaultModel();
model.setNsPrefix( "", ns );
Property hasAuthor = model.createProperty( ns + "hasAuthor" );
Resource rdfPrimer = model.createResource( "http://www.w3.org/TR/rdf-primer/" );
Resource fm = model.createResource( ns + "FrankManola" );
Resource em = model.createResource( ns + "EricMiller" );
rdfPrimer.addProperty( hasAuthor, fm );
rdfPrimer.addProperty( hasAuthor, em );
System.out.println( "== The Model ==" );
model.write( System.out, "N3" );
System.out.println( "\n== The Properties ==" );
StmtIterator it = rdfPrimer.listProperties( hasAuthor );
while( it.hasNext() ) {
Statement stmt = it.nextStatement();
System.out.println( " * "+stmt.getObject() );
System.out.println( " * "+stmt );
}
}
}
输出:
== The Model ==
@prefix : <http://www.example.com/> .
<http://www.w3.org/TR/rdf-primer/>
:hasAuthor :EricMiller , :FrankManola .
== The Properties ==
* http://www.example.com/EricMiller
* [http://www.w3.org/TR/rdf-primer/, http://www.example.com/hasAuthor, http://www.example.com/EricMiller]
* http://www.example.com/FrankManola
* [http://www.w3.org/TR/rdf-primer/, http://www.example.com/hasAuthor, http://www.example.com/FrankManola]