1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import org.xmldb.api.base.*; import org.xmldb.api.modules.*; import org.xmldb.api.*; import org.exist.xmldb.EXistResource; public class XPathExample { private static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc"; /** * args[0] Should be the name of the collection to access * args[1] Should be the XPath expression to execute */ public static void main(String args[]) throws Exception { final String driver = "org.exist.xmldb.DatabaseImpl"; // initialize database driver Class cl = Class.forName(driver); Database database = (Database) cl.newInstance(); database.setProperty("create-database", "true"); DatabaseManager.registerDatabase(database); Collection col = null; try { col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/"); XPathQueryService xpqs = (XPathQueryService)col.getService("XPathQueryService", "1.0"); xpqs.setProperty("indent", "yes"); ResourceSet result = xpqs.query("doc(\"/profesores.xml\")/profesores/profesor/nombre\n"); ResourceIterator i = result.getIterator(); Resource res = null; while(i.hasMoreResources()) { try { res = i.nextResource(); System.out.println(res.getContent()); } finally { //dont forget to cleanup resources try { ((EXistResource)res).freeResources(); } catch(XMLDBException xe) {xe.printStackTrace();} } } } finally { //dont forget to cleanup if(col != null) { try { col.close(); } catch(XMLDBException xe) {xe.printStackTrace();} } } } } |