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 50 |
import org.xmldb.api.base.*; import org.xmldb.api.modules.*; import org.xmldb.api.*; import org.exist.xmldb.EXistResource; public class XQueryExample { 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 XQuery 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/"); XQueryService xqs = (XQueryService) col.getService("XQueryService", "1.0"); xqs.setProperty("indent", "yes"); CompiledExpression compiled = xqs.compile("doc(\"/profesores.xml\")/profesores/profesor/nombre\n"); ResourceSet result = xqs.execute(compiled); 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();} } } } } |