Xindice - XPath to Query the Database |
This document describes how to
retrieve data from an Xindice XML database. Although this is a brief example, you should be able to execute this example. The example consist of: |
|
For any questions contact me at java @ groot . cc (remove the spaces in the e-mail address) Rik de Groot . |
Requirements |
|
XML
Document - example1.xml |
<?xml
version="1.0"?> <person xmlns:src="http://xml.apache.org/xindice/Query" src:col="/db/addressbook" src:key="address1"> <fname>Rik</fname> <lname>de Groot</lname> <phone type="work">+31351234567</phone> <phone type="home">+31357654321</phone> <email type="home">rikhome@provider</email> <email type="work">rikwork@provider</email> <address type="home">Street 45</address> <address type="work">Street 68</address> </person> |
Database
setup |
For
this example some XML data is required.
Database: 'db' initializing Script: 'GET' added to script storage Service: 'db' started Service: 'HTTPServer' started @ http://localhost:4080/ Service: 'APIService' started
Created : /db/addressbook
|
Java code - SimpleTest.java |
package
cc.groot.xindice; import org.xmldb.api.base.*; import org.xmldb.api.modules.*; import org.xmldb.api.*; /** * @author Rik de Groot */ public class SimpleTest { //-------------------------------------------------------------------- public SimpleTest() { try { initialize(); } catch (Exception anException) { System.out.println("Failed to initialize : " + anException); } } //-------------------------------------------------------------------- public static ResourceSet findByXpathQuery(Collection myCollection, String aXPath) throws XMLDBException { XPathQueryService myService = (XPathQueryService) myCollection.getService( "XPathQueryService", "1.0"); ResourceSet myResultSet = myService.query(aXPath); return myResultSet; } //-------------------------------------------------------------------- public static void initialize() throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException { String myDriver = "org.apache.xindice.client.xmldb.DatabaseImpl"; Class myDriverClass = Class.forName(myDriver); Database myDatabase = (Database) myDriverClass.newInstance(); DatabaseManager.registerDatabase(myDatabase); } //-------------------------------------------------------------------- public void printByLastName(String aLastName) throws XMLDBException { Collection myCollection = null; try { myCollection = DatabaseManager.getCollection( "xmldb:xindice:///db/addressbook"); ResourceSet myResultSet = findByXpathQuery(myCollection, "//person[lname='" + aLastName +"']"); ResourceIterator myResults = myResultSet.getIterator(); while (myResults.hasMoreResources()) { Resource myResource = myResults.nextResource(); System.out.println((String) myResource.getContent()); } } catch (XMLDBException myException) { System.err.println( "XML:DB Exception occured " + myException.errorCode); } finally { if (myCollection != null) { myCollection.close(); } } } //-------------------------------------------------------------------- public static void main(String[] args) { SimpleTest mySimpleTest = new SimpleTest(); try { mySimpleTest.printByLastName("de Groot"); } catch (XMLDBException anException) { System.err.println( "XML:DB Exception occured " + anException.errorCode); } } } |
Examples |
After executing the code the following data
will appear on the standard out. |
<?xml
version="1.0"?> <person xmlns:src="http://xml.apache.org/xindice/Query" src:col="/db/addressbook" src:key="address3"> <fname>Rik</fname> <lname>de Groot</lname> <phone type="work">+31351234567</phone> <phone type="home">+31357654321</phone> <email type="home">rikhome@provider</email> <email type="work">rikwork@provider</email> <address type="home">Street 45</address> <address type="work">Street 68</address> </person> |
Resources |
|