Modification Example

To illustrate document modification, we will:

  1. Retrieve a document named "doc1.xml" from a container.

  2. Rename an attribute node called 'attr1' to 'myAttribute'.

  3. Add a child node called "newChild" to node "node2".

  4. Remove a node called "removeNode".

  5. Update the contents of attribute node 'myAttribute' with the string "replacement content".

The document that we will update is as follows:

<sampleDocument>
    <node1 attr1="an attribute node" />
    <removeNode>Some content to remove</removeNode>
    <node2 />
</sampleDocument>

Notice that in performing the modification, we are not required to explicitly save the modified document back into the container; that is done for us under the covers.

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.XmlModify;
import com.sleepycat.dbxml.XmlUpdateContext;
import com.sleepycat.dbxml.XmlQueryContext;
import com.sleepycat.dbxml.XmlQueryExpression;
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");

    XmlQueryContext qc = myManager.createQueryContext();
    XmlUpdateContext uc = myManager.createUpdateContext();
    XmlModify mod = myManager.createModify();

    // Build the modification object.
    // Rename the attribute node from 'attr1' to 'myAttribute'.
    XmlQueryExpression select = 
        myManager.prepare("/sampleDocument/node1/@attr1", qc);
    mod.addRenameStep(select, "myAttribute");

    // Add '<newChild>' node to '<node2>'
    String newChildContent = "<c1>some content</c1>";
    select = myManager.prepare("/sampleDocument/node2", qc);
    mod.addAppendStep(select, 
                      XmlModify.Element, 
                      "newChild", 
                      newChildContent);

    // Remove <removeNode> from the document
    select = myManager.prepare("/sampleDocument/removeNode", qc);
    mod.addRemoveStep(select);

    // Replace the contents of /sampleDocument/node1/@myAttribute. Notice
    // the attribute was renamed from attr1 in the first step of this
    // modification. Modifications are performed in the specified order.
    String attrContent = "replacement content";
    select = myManager.prepare("/sampleDocument/node1/@myAttribute", qc);
    mod.addUpdateStep(select, attrContent);

    // Now retrieve the document we want to modify from the container. 
    // Notice that we could have performed a query against the container,
    // and then handed the entire result set to this method. In that case,
    // every document contained in the result set is modified.
    XmlDocument retDoc = myContainer.getDocument("doc1.xml");
    XmlValue docValue = new XmlValue(retDoc);
    mod.execute(docValue, qc, uc);

    // Show that the modification was performed
    // and written to the container.
    XmlDocument retDoc2 = myContainer.getDocument("doc1.xml");

    String mssg = retDoc2.getName() + ":\n";
    mssg += retDoc2.getContentAsString();
    System.out.println(mssg);

    select.delete();
    mod.delete();
    qc.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
    } 

When we run this code, the program displays the modified document which is now:

doc1.xml:
<sampleDocument>
    <node1 myAttribute="replacement content" />
    
    <node2><newChild><c1>some content</c1></newChild></node2>
</sampleDocument>