Chapter 6. Managing Documents in Containers

Table of Contents

Deleting Documents
Replacing Documents
Modifying XML Documents
XQuery Update Introduction
Inserting Nodes Using XQuery Update
Deleting Nodes Using XQuery Update
Replacing Nodes Using XQuery Update
Renaming Nodes Using XQuery Update
Updating Functions
Transform Functions
Resolving Conflicting Updates
Compressing XML Documents
Turning Compression Off
Using Custom Compression

BDB XML provides APIs for deleting, replacing, and modifying documents that are stored in containers. This chapter discusses these activities.

Deleting Documents

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.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

    XmlDocument theDoc = myManager.createDocument();
    while (results.next(theDoc)) {
        myContainer.deleteDocument(theDoc);
    }
    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
    }