Replacing Documents

You can either replace a document in its entirety as described here, or you can modify just portions of the document as described in Modifying XML Documents.

If you already have code in place to perform document modifications, then replacement is the easiest mechanism to implement. However, replacement requires that at least the entire replacement document be held in memory. Modification, on the other hand, only requires that the portion of the document to be modified be held in memory. Depending on the size of your documents, modification may prove to be significantly faster and less costly to operate.

You can directly replace a document that exists in a container. To do this:

  1. Retrieve the document from the container. Either do this using an XQuery query and iterating through the results set looking for the document that you want to replace, or use XmlContainer.getDocument() to retrieve the document by its name. Either way, make sure you have the document as an XmlDocument object.

  2. Use XmlDocument.setContent() or XmlDocument.setContentAsXmlInputStream() to set the object's content to the desired value.

  3. Use XmlContainer.updateDocument() to save the modified document back to the container.


Alternatively, you can create a new blank document using XmlManager.createDocument(), set the document's name to be identical to a document already existing in the container, set the document's content to the desired content, then call XmlContainer.updateDocument().

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;

XmlManager myManager = null;
XmlContainer myContainer = null;


try {
    // Get a manager object.
    myManager = new XmlManager();

    // Open a container
    myContainer = 

    // Document to modify
    String docName = "doc1.xml";
    XmlDocument theDoc = myContainer.getDocument(docName);

    // Modify it
    theDoc.setContent("<a><b>random content</a></b>");

    // Put it back into the container

} catch (XmlException e) {
    // Error handling goes here.
} finally {
    try {
        if (myContainer != null) {

        if (myManager != null) {
    } catch (XmlException ce) {
        // Exception handling goes here