Table of Contents
BDB XML provides APIs for deleting, replacing, and modifying documents that are stored in containers. This chapter discusses these activities.
You can delete a document by calling XmlContainer.deleteDocument(). This method can operate either on a document's name or on an XmlDocument object. You might want to use an XmlDocument object to delete a document if you have queried your container for some documents and you want to delete every document in the results set.
For example:
package dbxml.gettingStarted;
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlDocument;
import com.sleepycat.dbxml.XmlException;
import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlQueryContext;
import com.sleepycat.dbxml.XmlResults;
import com.sleepycat.dbxml.XmlUpdateContext;
import com.sleepycat.dbxml.XmlValue;
...
XmlManager myManager = null;
XmlContainer myContainer = null;
...
try {
// Get a manager object.
myManager = new XmlManager();
// Open a container
myContainer =
myManager.openContainer("exampleData.dbxml");
// Get a query context
XmlQueryContext context = myManager.createQueryContext();
// Declare a namespace
context.setNamespace("fruits", "http://groceryItem.dbxml/fruits");
// Declare the query string. Find all the product documents
// in the fruits namespace.
String myQuery = "collection('exampleData.dbxml')/fruits:product";
// Perform the query.
XmlResults results = myManager.query(myQuery, context);
// Delete everything in the results set
XmlUpdateContext uc = myManager.createUpdateContext();
XmlDocument theDoc = myManager.createDocument();
while (results.next(theDoc)) {
myContainer.deleteDocument(theDoc, uc);
}
results.delete();
context.delete();
} catch (XmlException e) {
// Error handling goes here.
} finally {
try {
if (myContainer != null) {
myContainer.close();
}
if (myManager != null) {
myManager.close();
}
} catch (XmlException ce) {
// Exception handling goes here
}