This is the class I use to access dbxml. All your answers are
contained in here :-).
Mark
--- Begin Message ---
package com.cybershopmgr.server.dbmgr;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
// For the dbXML specific CollectionManager service
import org.dbxml.client.xmldb.services.*;
import org.dbxml.xml.dom.*;
/**
* Description of the Class
*
[EMAIL PROTECTED] Mark J Stang
[EMAIL PROTECTED] November 17, 2001
*/
public class Crud
{
private Collection col = null;
private String _collectionName = "xmldb:dbxml:///db";
private String driver = "org.dbxml.client.xmldb.DatabaseImpl";
private Database database = null;
private XMLResource document = null;
private ResourceSet resultSet = null;
public Crud()
{
initialize();
}
/**
* Constructor for the Crud object
*
[EMAIL PROTECTED] collectionName Description of Parameter
*/
public Crud(String collectionName)
{
if (collectionName != null)
{
if (collectionName.length() > 0)
{
_collectionName = "xmldb:dbxml:///db/" + collectionName;
}
}
initialize();
}
/**
* Search using XPath
*
[EMAIL PROTECTED] xpath Description of Parameter
[EMAIL PROTECTED] Description of the Returned Value
*/
public ResourceSet search(String xpath) throws DocumentNotFound
{
try
{
col = DatabaseManager.getCollection(_collectionName);
if (col != null)
{
XPathQueryService service = (XPathQueryService)
col.getService("XPathQueryService", "1.0");
resultSet = service.query(xpath);
}
}
catch (XMLDBException e)
{
e.printStackTrace();
throw new DocumentNotFound("XML:DB Exception occured " +
e.errorCode);
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
return resultSet;
}
/**
* Create a new Collection
*
[EMAIL PROTECTED] newCollectionName Description of Parameter
[EMAIL PROTECTED] CollectionException Description of Exception
*/
public void createCollection(String newCollectionName) throws
CollectionException
{
try
{
col = DatabaseManager.getCollection("xmldb:dbxml:///db");
if (col != null)
{
CollectionManager service = (CollectionManager)
col.getService("CollectionManager", "1.0");
// Build up the Collection XML configuration.
String collectionConfig =
"<collection compressed=\"true\" name=\"" +
newCollectionName + "\">" +
" <filer
class=\"org.dbxml.core.filer.BTreeFiler\" gzip=\"true\"/>" +
"</collection>";
service.createCollection(newCollectionName,
DOMParser.toDocument(collectionConfig));
}
}
catch (XMLDBException e)
{
throw (new CollectionException("XML:DB Exception occured " +
e.errorCode + " " + e.getMessage()));
}
catch (org.dbxml.server.dbXMLException e)
{
throw (new CollectionException("dbXMLException occured " +
e.getMessage()));
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
}
public void deleteCollection(String collectionName) throws
CollectionException
{
try
{
col = DatabaseManager.getCollection("xmldb:dbxml:///db");
CollectionManager service = (CollectionManager)
col.getService("CollectionManager", "1.0");
service.dropCollection(collectionName);
}
catch (XMLDBException e)
{
throw (new CollectionException("XML:DB Exception occured " +
e.errorCode + " " + e.getMessage()));
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
}
/**
* Delete a document, based on a Key
*
[EMAIL PROTECTED] key Description of Parameter
[EMAIL PROTECTED] DocumentErrorException Description of Exception
*/
public void delete(String key) throws DocumentErrorException
{
try
{
col = DatabaseManager.getCollection(_collectionName);
if (col != null)
{
Resource document = col.getResource(key);
col.removeResource(document);
}
}
catch (XMLDBException e)
{
throw new DocumentErrorException("XML:DB Exception occured "
+ e.errorCode + " " + e.getMessage());
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
}
/**
* Read a document, based on key
*
[EMAIL PROTECTED] key Description of Parameter
[EMAIL PROTECTED] Description of the Returned Value
[EMAIL PROTECTED] DocumentNotFound Description of Exception
*/
public String read(String key) throws DocumentNotFound
{
String returnString = null;
XMLResource document = null;
try
{
col = DatabaseManager.getCollection(_collectionName);
if (col != null)
{
document = (XMLResource) col.getResource(key);
}
if (document == null)
{
throw (new DocumentNotFound("Document" + key + "Not
Found"));
}
returnString = document.getContent().toString();
}
catch (org.xmldb.api.base.XMLDBException e)
{
e.printStackTrace();
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
return (returnString);
}
/**
* Add a new XML Document to the collection
*
[EMAIL PROTECTED] newDocument Description of Parameter
[EMAIL PROTECTED] Description of the Returned Value
[EMAIL PROTECTED] DocumentErrorException Description of Exception
*/
public String add(String newDocument) throws DocumentErrorException
{
String resultId = null;
try
{
col = DatabaseManager.getCollection(_collectionName);
if (col != null)
{
XMLResource document = (XMLResource)
col.createResource(null, "XMLResource");
document.setContent(newDocument);
col.storeResource(document);
resultId = document.getId();
}
}
catch (org.xmldb.api.base.XMLDBException e)
{
//e.printStackTrace();
throw (new DocumentErrorException(e.getMessage()));
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
return resultId;
}
public long update(String key, String xupdate)
{
long rc = 0;
try
{
col = DatabaseManager.getCollection(_collectionName);
XUpdateQueryService service = (XUpdateQueryService)
col.getService("XUpdateQueryService", "1.0");
rc = service.updateResource(key, xupdate);
}
catch (XMLDBException e)
{
System.err.println("XML:DB Exception occured " + e.errorCode
+ " " +e.getMessage());
}
finally
{
if (col != null)
{
try
{
col.close();
}
catch (XMLDBException e)
{
System.err.println("Error closing
collection...XML:DB Exception occured " + e.errorCode + " " +
e.getMessage());
}
}
}
return rc;
}
/**
* Set up the DatabaseManager
*/
public void initialize()
{
try
{
Class c = Class.forName(driver);
Database database = (Database) c.newInstance();
DatabaseManager.registerDatabase(database);
}
catch (org.xmldb.api.base.XMLDBException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
System.out.println("ClassNotFoundException=" + e);
}
catch (IllegalAccessException e)
{
System.out.println("IllegalAccessException=" + e);
}
catch (InstantiationException e)
{
System.out.println("InstantiationException=" + e);
}
}
}
--- End Message ---