/** * This method performs an XPath query to the database and returns the results as a Vector of * Strings. * * @param collection The name of the collection to look for the document. * @param query A string containing an XPath expression which shall act as a query against the * database. * @param username The identifier of the user calling the method used for authentication. * @param password The password of the user calling the method used for authentication. * @return A Vector containing the answers to the query as Strings. */ public Vector query(String collection, String query, String username, String password) { Vector response = new Vector(); try { Class cl = Class.forName(_driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection(_URI + collection, username, password); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("indent", "yes"); ResourceSet result = service.query(query); ResourceIterator i = result.getIterator(); while (i.hasMoreResources()) { Resource r = i.nextResource(); response.add((String) r.getContent()); } } catch (Exception e) { e.printStackTrace(); } return response; }
public static void main(String args[]) throws Exception { String driver = "org.exist.xmldb.DatabaseImpl"; Class cl = Class.forName(driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection("xmldb:exist:///db", "admin", ""); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("indent", "yes"); ResourceSet result = service.query( "for $s in //intervention/(speaker|writer)/affiliation[@EPparty ='PSE'] return data($s/../../(speech|writing)/@ref)"); ResourceIterator i = result.getIterator(); while (i.hasMoreResources()) { Resource r = i.nextResource(); System.out.println((String) r.getContent()); } // shut down the database DatabaseInstanceManager manager = (DatabaseInstanceManager) col.getService("DatabaseInstanceManager", "1.0"); manager.shutdown(); }