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 javax.xml.transform.OutputKeys; import org.exist.xmldb.EXistResource; public class QueryExample { private static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc/db/"; /** * args[0] Should be the name of the collection to access * args[1] Should be the name of the resource to read from the collection */ 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; XMLResource res = null; try { // get the collection col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/"); col.setProperty(OutputKeys.INDENT, "no"); res = (XMLResource)col.getResource("profesores.xml"); if(res == null) { System.out.println("document not found!"); } else { System.out.println(res.getContent()); } } finally { //dont forget to clean up! if(res != null) { try { ((EXistResource)res).freeResources(); } catch(XMLDBException xe) {xe.printStackTrace();} } if(col != null) { try { col.close(); } catch(XMLDBException xe) {xe.printStackTrace();} } } } } |