import com.hp.hpl.jena.rdf.model.*; import java.io.*; public class example1 extends Object { public static void main (String args[]) { Model model = ModelFactory.createDefaultModel(); // use the class loader to find the input file InputStream in = example1.class .getClassLoader() .getResourceAsStream("foaf.rdf"); if (in == null) { throw new IllegalArgumentException( "File: not found"); } // read the RDF/XML file - the URI is used to turn relative URLS // into absolute URLs model.read(new InputStreamReader(in), "http://www.cs.usfca.edu/"); //print out the model as a set of triples StmtIterator iter = model.listStatements(); // print out the predicate, subject and object of each statement while (iter.hasNext()) { Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = 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() + "\""); } } // Now let's find all the 'knows' nodes Property knows = model.getProperty(FOAF.getURI(), "knows"); NodeIterator niter = model.listObjectsOfProperty(knows); while(niter.hasNext()){ RDFNode node = niter.nextNode(); } // write it to standard out model.write(System.out); } }