// list the statements in the Model StmtIteratoriter= model.listStatements();
// print out the predicate, subject and object of each statement while (iter.hasNext()) { Statementstmt= iter.nextStatement(); // get next statement Resourcesubject= stmt.getSubject(); // get the subject Propertypredicate= stmt.getPredicate(); // get the predicate RDFNodeobject= stmt.getObject(); // get the object
System.out.print(subject.toString()); System.out.print(" " + predicate.toString() + " "); if (object instanceof Resource) { System.out.print(object.toString()); } else { // object is a literal System.out.print(" \"" + object.toString() + "\""); }
System.out.println(" ."); }
由于object可以是Resource或者Literal,所以object的类型为RDFNode。
Writing RDF
1 2 3 4
// now write the model in XML form to a file model.write(System.out); model.write(System.out, "RDF/XML-ABBREV"); // write the model in XML form to a file model.write(System.out, "N-TRIPLES"); // write the model in N-TRIPLES form to a file
Reading RDF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// create an empty model Modelmodel= ModelFactory.createDefaultModel();
// use the FileManager to find the input file InputStreamin= FileManager.get().open( inputFileName ); if (in == null) { thrownewIllegalArgumentException( "File: " + inputFileName + " not found"); }
// read the RDF/XML file model.read(in, null);
// write it to standard out model.write(System.out);