to parse .rdfs file
below is my .rdfs file. i need to get YYYY if my input is XXXX. How do i do it.
<rdfs:Class rdf:about="&kb;XXXX"
rdfs:label="XXXX">
<rdfs:subClassOf rdf:resource="&kb;YYYY"/>
</rdfs:Class>
my code:
StmtIterator iter = model.listStatements(
new
SimpleSelector(null, RDFS.label, (RDFNode) null) {
@Over开发者_如何学编程ride
public boolean selects(Statement s) {
return s.getString().endsWith("XXXX");
}
});
if (iter.hasNext()) {
System.out.println("The database contains:");
while (iter.hasNext()) {
System.out.println(" " + iter.nextStatement()
.getString());
}
There's absolutely no need to search the entire Model
every time you need to access a triple. Try:
String kb = "..."; // whatever &kb; expands to
Resource subj = model.getResource( kb + "XXXX" );
Resource super = subj.getProperty( RDFS.subClassOf )
.getResource();
It's even easier if you use the ontology API:
OntModel m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
m.read( ... your file: or http: URL here );
OntClass x = m.getOntClass( NS + "XXX" );
OntClass y = x.getSuperClass();
精彩评论