QuickStart.java


// QuickStart.java
//
// o Gets a the root collection of the Sedna XML Database.
// o Creates a collection called myCollection.
// o Creates a collection called mySub within myCollection.
// o Inserts 50 random XML Documents in collection mySub.
// o Runs an XQuery over mySub collection, prints the results.
// o Deletes collection myCollection and everything underneath it.
// o Closes the collection.

import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;

public class QuickStart
{
// --------------------
// Change these details
// --------------------
private String databaseURI      = "xmldb:sedna://localhost/db";
private String databaseUsername = "SYSTEM";
private String databasePassword = "MANAGER";
// --------------------

public static void main(String[] args)
{
  QuickStart qs = new QuickStart();
  qs.execute();
}

public void execute()
{
  try
  {
    registerXMLDBDriver();

    Collection rootCollection = DatabaseManager.getCollection(
      databaseURI, databaseUsername, databasePassword);

    Collection myCollection = createChildCollection(
      rootCollection, "myCollection");

    Collection mySub = createChildCollection(myCollection,
      "mySub");

    for(int i=0;i<50;i++)
    {
      String xmlDocument = "<myDoc>\n" +
      "<id>" + i + "</id>\n" +
      "<data>" + (Math.random()*1000000) + "</data>\n" +
      "</myDoc>";

      XMLResource resource=(XMLResource)mySub.createResource(null,
        XMLResource.RESOURCE_TYPE);

      resource.setContent(xmlDocument);

      mySub.storeResource(resource);
    }

    XQueryService queryService =
      (XQueryService)mySub.getService("XQueryService","1.0");

    ResourceSet resourceSet =
    queryService.query("for $x in //myDoc\n"+
                       "return <result>id = {$x/id/text()}, "+
                       "data = {$x/data/text()}</result>");

    System.out.println("The results were as follows:");
    System.out.println("----------------------------");
    ResourceIterator iterator = resourceSet.getIterator();
    while(iterator.hasMoreResources())
    {
      Resource resource = iterator.nextResource();
      System.out.println(resource.getContent());
    }
    System.out.println("----------------------------");

    deleteCollection(rootCollection, "myCollection");

    rootCollection.close();
  }
  catch(XMLDBException e)
  {
    e.printStackTrace();
  }
}


public void registerXMLDBDriver() throws XMLDBException
{
  try
  {
    Database dbDriver =
    (Database)Class.forName(
      "net.cfoster.sedna.DatabaseImpl").newInstance();
    DatabaseManager.registerDatabase(dbDriver);
  }
  catch(ClassNotFoundException e) {
    System.err.println("ClassNotFoundException: "+
      e.getMessage());
  }
  catch(InstantiationException e) {
    System.err.println("InstantiationException: "+
      e.getMessage());
  }
  catch(IllegalAccessException e) {
    System.err.println("IllegalAccessException: "+
      e.getMessage());
  }
}

public Collection createChildCollection(Collection collection,
  String collectionName) throws XMLDBException
{
  CollectionManagementService cms =
    (CollectionManagementService)collection.getService(
      "CollectionManagementService","1.0");
  return cms.createCollection(collectionName);
}

public void deleteCollection(Collection collection,
  String collectionName) throws XMLDBException
{
  CollectionManagementService cms =
  (CollectionManagementService)collection.getService(
    "CollectionManagementService","1.0");
  cms.removeCollection(collectionName);
}

}