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:

  • Requirements
  • XML Document
    • example1.xml
  • Database setup
  • Java code
    • SimpleTest.java
  • Examples
  • Resources

For any questions contact me at java @ groot . cc (remove the spaces in the e-mail address)

Rik de Groot .
Requirements
  • Java2
  • Xindice 1.0
    • xindice.jar - contains the main Xindice classes that are used by the client API.
    • xmldb.jar - contains the XML:DB API interfaces.
    • openorb-1.2.0.jar - contains the CORBA ORB implementation used by the client API to communicate with the server.
    • xerces-1.4.3.jar - contains the Xerces XML parser.
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.
  • Start the Xindice Database
Xindice 1.0 (Birthday)

Database: 'db' initializing
Script: 'GET' added to script storage
Service: 'db' started
Service: 'HTTPServer' started @ http://localhost:4080/
Service: 'APIService' started
  • Create a collection
xindiceadmin ac -c /db -n addressbook
Created : /db/addressbook
  • Add the document to the database
xindice ad -c /db/addressbook -f address1.xml -n address1
Added document /db/addressbook/address1

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
©2003, Rik de Groot