If you study the ontology file (. OWL), you will find that all of them are composed of three tuples. However, both subject, predicte, and object are described as resources, and the resource identifier is Uri, therefore, there will always be a large string of prefixes. What we are interested in is often the last word, which is called localname. I don't know why. How can we get the triple represented by localname? Subject and predicte can be obtained directly through getlocalname, while object is annoying, without the getlocalname Function ~ Check the statement structure (resource arg0, property arg1, rdfnode arg2). The problem is that the rdfnode type does not have the getlocalname function, but you can convert it to a resource by calling the asresource function. Code:
Fileinputstream file = new
Fileinputstream (owl_path); // Of course, owl_path is the path of the owl file.
Inputstreamreader in = new
Inputstreamreader (file, "UTF-8"); // solves Chinese problems
Model =
Modelfactory. createdefamodel model ();
Model. Read (in, null );
Required titerator stmiter = model. liststatements ();
While (stmiter. hasnext ()){
Statement STM = stmiter. nextstatement ();
Rdfnode object = STM. GetObject ();
If (Object instanceof Resource) {// mainly to determine whether rdfnode is a resource or a literal
System. out. println (STM. getsubject (). getlocalname () + "**" + STM. getpredicate (). getlocalname () + "**" + object. asresource (). getlocalname ());
}
Else
System. out. println (STM. getsubject (). getlocalname () + "**" + STM. getpredicate (). getlocalname () + "**" + object. tostring ());
}