Xml-db api
From GEANT2-JRA1 Wiki
Contents |
[edit]
XML:DB API with eXist native XML database
[edit]
Connecting to the database (and collection)
- Selecting driver (eXist)
Class cl = Class.forName("org.exist.xmldb.DatabaseImpl"); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase(database);
- Opening a collection
Collection col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/collection");
- or (with username and password)
Collection col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/collection","user", "password");
[edit]
Querying
- Performing XPath or XQuery
XPathQueryService service =
(XPathQueryService) col.getService("XPathQueryService", "1.0");
service.setProperty("indent", "yes");
String query = "for $a in //host[@name='google'] return $a/name";
ResourceSet result = service.query(query);
- Viewing results
ResourceIterator i = result.getIterator(); while(i.hasMoreResources()) { Resource r = i.nextResource(); System.out.println((String)r.getContent()); }
[edit]
Data modifications
- by XQuery (eXist XQuery update extension); works with Development Snapshot -- eXist-snapshot-20050801.jar (!)
XPathQueryService service =
(XPathQueryService) col.getService("XPathQueryService", "1.0");
service.setProperty("indent", "yes");
String query = "for $a in //host[@id='buttercup'] "
+"return update replace $a/name "
+"with <name>BuTTerCuP</name>";
ResourceSet result = service.query(query);
- by XUpdate
XUpdateQueryService service =
(XUpdateQueryService)col.getService("XUpdateQueryService", "1.0");
service.setCollection(col);
String xupdate =
"<xu:modifications version=\'1.0\' " +
" xmlns:xu=\'http://www.xmldb.org/xupdate\'>" +
" <xu:insert-after select='//host[1]'>" +
" <host id='new'>" +
" <name>NewHost</name>" +
" </host>" +
" </xu:insert-after>" +
"</xu:modifications>";
service.update(xupdate);
[edit]
