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.

Note

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:

#include "DbXml.hpp"
...

using namespace DbXml;

...

// Get a manager object.
XmlManager myManager;

// Open a container
XmlContainer myContainer = 
    myManager.openContainer("exampleData.dbxml");

// Document to modify
std::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
XmlUpdateContext uc = myManager.createUpdateContext();
myContainer.updateDocument(theDoc, uc);