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.

#include "DbXml.hpp"
...

using namespace DbXml;

...

// Get a manager object.
XmlManager myManager;

// Open a container
XmlContainer 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>'
std::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.
std::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(retDoc);
mod.execute(docValue, qc, uc);

// Show that the modification was performed
// and written to the container.
XmlDocument retDoc2 = myContainer.getDocument("doc1.xml");
std::string doc1String;
std::cout << retDoc2.getName() << ":\n" 
          << retDoc2.getContent(doc1String) 
          << "\n\n" << std::endl; 

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>