This article is more than 1 year old

An embedded XML Database: Oracle Berkeley DB XML

There is more to life than RDBMS

Modifying XML Documents

Next, we’ll modify an XML document in the database. BDB XML API provides the XmlModify class to modify an XML document and the procedure to modify an XML document is as follows:

  1. Create an XmlModify object.
  2. Select the nodes to be modified.
  3. Specify the modification steps. Modifications are performed in the order specified.
  4. Run the modifications in the context of an XML document or a set of XML documents.

As in the previous sections, first create an XmlModify object, an XmlQueryContext object, and an XmlUpdateContext object. As an example, add a section attribute to the article element. Select the article node using an XQuery expression:

XmlQueryExpression select = xmlManager.prepare("/catalog/journal/article",qc);

Use the addAppendStep() method to append the section attribute to article element. The type of an object to be added may be an element (represented with XmlModify.Element), an attribute (XmlModify.Attribute), a comment (XmlModify.Comment), text (XmlModify.Text), or a processing instruction (XmlModify.ProcessingInstruction). We’ll add a section attribute with value “Developer”:

mod.addAppendStep(select, XmlModify.Attribute, "section", "Developer");

Next, add a journal element after the journal element in catalog1 document. Select the journal node in catalog1 document:

XmlQueryExpression 
 select = xmlManager.prepare("/catalog/journal 
 [article/title='Using Bind Variables']",qc);

Specify the element content to be added:

String objectContent = "<article>…</article>";

Add the journal element using the addInsertAfterStep() method:

mod.addInsertAfterStep(select, XmlModify.Element, "journal",objectContent);

The modifications are not complete yet. Obtain the XML document in which the modification is to be performed:

XmlDocument xmlDocument = xmlContainer.getDocument("catalog1");

Obtain the XmlValue object for the XML document and run the modifications:

XmlValue xmlValue = new XmlValue(xmlDocument);
 mod.execute(xmlValue, qc, uc);

Similarly, elements may be updated, renamed, and deleted.

The Berkeley DB XML database has advantages over a relational database for storing complete XML documents that may be queried, modified, and updated without having to retrieve the documents from the database. ®

More about

TIP US OFF

Send us news


Other stories you might like