Listing 1 (Java snippet shows how to configure the Environment)

//Environment configuration goes into this Object
EnvironmentConfig envConf = new EnvironmentConfig();

//If the environment does not exit, create it.
envConf.setAllowCreate(true);

//Turn on shared memory region
envConf.setInitializeCache(true);

//Turn on locking
envConf.setInitializeLocking(true);

//Turn on logging
envConf.setInitializeLogging(true);

//Enable transactions
envConf.setTransactional(true);

//create the Environment Object
Environment environment = new Environment(envHome, envConf);

Listing 2 (Creating a container and adding a document)

//open the environment to work on it
environment = new Environment(envHome, envConf);
XmlManagerConfig managerConfig = new XmlManagerConfig();
managerConfig.setAdoptEnvironment(true);
managerConfig.setAllowAutoOpen(true);
managerConfig.setAllowExternalAccess(true);
xmlManager = new XmlManager(environment, managerConfig);

			
//let's create a container named xbench.dbxml
xmlManager.createContainer("xbench.dbxml");
xContainer =  xmlManager.openContainer("xbench.dbxml");

//Get the input stream.
String fileName = "C:/dictionary10.xml";

//Give it a unique name
String docName = "dictionary10";

//Need an update context for the put.
XmlUpdateContext theContext = xmlManager.createUpdateContext();

XmlInputStream xStream = xmlManager.createLocalFileInputStream(fileName);

xContainer.putDocument(new XmlTransaction(), docName, xStream, theContext);


Listing 3 (Querying data)

import java.io.File;
import java.io.FileNotFoundException;

import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.dbxml.*;

/**
 * @author Selim Mimaroglu
 *
 * written for Berkeley DB XML v2.2
 *
 * This class queries the database.
 * 
 * This code is presented as is. If you decide to use this code, you do so at your own risk;
 * the author takes no responsibility for loss or harm of any kind.
 * 
 */
public class Query_XML {

	public static void main(String[] args) {
		XmlManager xmlManager = null;
		Environment environment = null;
		EnvironmentConfig envConf = null;
		//the database environment is stored under this directory
		File envHome = new File("D:/bdb_xml_testenv");
		XmlContainer xmlContainer = null;

		try {
			//Configure the database environment
			//Don't forget to use the original configuration created
			//when the environment was initialized
			envConf = new EnvironmentConfig();
			envConf.setAllowCreate(true);
			envConf.setInitializeCache(true);
			envConf.setInitializeLocking(true);
			envConf.setInitializeLogging(true);
			envConf.setTransactional(true);

			environment = new Environment(envHome, envConf);

			//Create XmlManager and XmlContainer
			//Set up the XmlManager configuration
			XmlManagerConfig managerConfig = new XmlManagerConfig();
			managerConfig.setAdoptEnvironment(true);
			managerConfig.setAllowAutoOpen(true);
			managerConfig.setAllowExternalAccess(true);
			xmlManager = new XmlManager(environment, managerConfig);
			xmlContainer = xmlManager.openContainer("xbench.dbxml");
			XmlQueryContext context = xmlManager.createQueryContext();

			String query = "collection('xbench.dbxml')/dictionary/e/hwg/hw";

			//Perform the query.
			XmlResults results = xmlManager.query(query, context);

			//Print the results
			String message = new String("Found " + results.size() +
			" results for the query: " + query );
			System.out.println(message);

			//Loop over the result set
			while (results.hasNext()) {
				XmlValue value = results.next();
				System.out.println(value.asString());
			}
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DatabaseException e) {
			e.printStackTrace();
		} finally {
			if(xmlContainer != null ) {
				try {
					xmlContainer.close();
				} catch (XmlException e1) {
					e1.printStackTrace();
				}
			}
		}
		
	}
}


Listing 4 (Full code, some of it is provided in Listings 1 and 2)

import java.io.File;
import com.sleepycat.db.*;
import com.sleepycat.dbxml.*;

/**
 * @author Selim Mimaroglu
 *
 * written for Berkeley DB XML v2.2
 *
 * Some portions of the code are inspired from "Getting Started with Berkeley
 * DB XML for Java"
 *
 * This class creates the following Database Objects:
 * - Environment
 * - XmlManager
 * - Container
 * 
* This code is presented as is. If you decide to use this code, you do so at your own risk;
 * the author takes no responsibility for loss or harm of any kind.
 * 
 */
public class Create {

	/**
	 * This the Environment variable
	 * We have to create an environment, once and only once
	 */
	private Environment environment;
	/**
	 * XmlManager manages containers, creates input streams, runs queries
	 * and more
	 */
	private XmlManager xmlManager;

	/**
	 * We are going to create an environment in  this location
	 */
	private File envHome;

	/**
	 * After creating an environment, use the same configuration to
	 * open it
	 */
	private EnvironmentConfig envConf;

	/**
	 * Default Constructor Initializes the variables
	 *  
	 */
	public Create() {
		environment = null;
		xmlManager = null;
		//change this for your needs
		envHome = new File("D:/bdb_xml_testenv");
		envConf = null;
	}

	/**
	 * This method creates an environment.
	 * You must create an environment with some properties (like below)
	 *  in order to work with the Berkeley DB XML
	 */
	public void createEnvironment() {

		try {
			//Environment configuration goes into this Object
			envConf = new EnvironmentConfig();

			//If the environment does not exit, create it.
			envConf.setAllowCreate(true);

			//Turn on the shared memory region
			envConf.setInitializeCache(true);

			//Turn on locking
			envConf.setInitializeLocking(true);

			//Turn on logging
			envConf.setInitializeLogging(true);

			//Enable transactions
			envConf.setTransactional(true);

			//initialize the environment
			environment = new Environment(envHome, envConf);

		} catch (Exception e) {
			//For now this just prints out the Exception message
			//Include Exception handling action here
			System.err.println(e.getMessage());
		} finally {
			try {
				//Don't forget closing the environment after you are done
				if (environment != null) {
					environment.close();
				}
			} catch (DatabaseException e) {
				System.err.println(e.getMessage());
			}
		}

	}
	
	/**
	 * This method creates an XmlManager instance with the requested
	 * specifications
	 *
	 */
	public void createXmlManager() {

		// if the environment is not created, create it first
		if(environment == null) {
			this.createEnvironment();
		}

		XmlContainer xContainer = null;

		try {
			//open the environment to work on it
			environment = new Environment(envHome, envConf);
			XmlManagerConfig managerConfig = new XmlManagerConfig();
			managerConfig.setAdoptEnvironment(true);
			managerConfig.setAllowAutoOpen(true);
			managerConfig.setAllowExternalAccess(true);
			xmlManager = new XmlManager(environment, managerConfig);

			
			//let's create a container named xbench.dbxml
			xmlManager.createContainer("xbench.dbxml");
			xContainer =  xmlManager.openContainer("xbench.dbxml");

			//Get the input stream.
			String fileName = "C:/dictionary10.xml";

			//Give it a unique name
			String docName = "dictionary10";

			//Need an update context for the put.
			XmlUpdateContext theContext = xmlManager.createUpdateContext();

			XmlInputStream xStream = xmlManager.createLocalFileInputStream(fileName);
			xContainer.putDocument(new XmlTransaction(), docName, xStream, theContext);

		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {

			try {
				//close the container when done
				if(xContainer != null) {
					xContainer.close();
				}
				if (xmlManager != null) {
					xmlManager.close();
				}
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
		}
		
		
		
	}
	
	
	public static void main(String[] args) {
		Create create = new Create();
		create.createEnvironment();
		create.createXmlManager();
	}
}