XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Under the Hood: Oracle Berkeley DB XML
by Deepak Vohra | Pages: 1, 2

Adding XML Documents

Next, you will add XML documents to the BDB XML database. Similar to the putDocument shell command, the BDB XML API provides a method putDocument() to store an XML document in a database. Create a String object that represents am XML document and specify a document name.

String docString =
"<catalog title='Oracle Magazine' publisher=
'Oracle Publishing'>" +
"<journal date='March-April 2006'>" + 
"<article>" +
"<title>Using Bind Variables</title>" +
"<author> Steve Muench </author>" + 
"</article>" +
"</journal>" + "</catalog>";
 String docName = "catalog1";

Create an XmlUpdateContext object. An XmlUpdateContext object represents the context within which update operations are performed in a container.

XmlUpdateContext updateContext = 
xmlManager.createUpdateContext();

Store the XML document using the putDocument method.

xmlContainer.putDocument(docName, docString,
 updateContext, null);

Similarly, add another XML document.

Querying XML Documents with XQuery

Next, you query the XML documents in the catalog.dbxml BDXML database using XQuery. XQuery is a SQL-like query language for XML and is based on XPath. First, create a XmlQueryContext object. A XmlQueryContext object represents the context within which a XML document in a container is queried.

XmlQueryContext context = 
xmlManager.createQueryContext(); 

As an example query, retrieve the values of all the titles in the BDB XML database. Specify the query string that represents the XQuery expression for the query.

String query = "collection('catalog.dbxml')/catalog/
journal/article/title/text()";

Compile the XQuery expression into an XmlQueryExpression object. using the prepare method of XmlManager class.

XmlQueryExpression qe = xmlManager.prepare(query, context); 

Next, evaluate the XQuery expression using execute method of XmlQueryExpression object. When the XQuery query is run the results are represented by the XmlResults object.

XmlResults results = qe.execute(context);

Iterate over the results and output the titles retrieved.

while (results.hasNext()) {
     XmlValue xmlValue = results.next();
     System.out.println(xmlValue.asString());
   }

The output from the query is as follows.

Using Bind Variables
From Application Express to XE

Modifying XML Documents

Next, you can modify an XML document in the database, using the XmlModify class provided by BDB XML API to modify an XML document. The procedure to modify an XML document is as follows.

  1. Create a 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.

First, create a XmlModify object.

XmlModify mod = xmlManager.createModify();

Also, create a XmlQueryContext object, which represents the context in which a XQuery query is performed to select nodes for modification, and an XmlUpdateContext object, which represents the context within which update operations are performed.

XmlQueryContext qc = 
xmlManager.createQueryContext();
XmlUpdateContext uc = 
xmlManager.createUpdateContext();

The XmlModify class provides various methods to modify an XML document. These methods are discussed in Table 3.

Method Description
addAppendStep Appends the provided data to a specified node’s child nodes as an element, attribute, comment, text, or processing instruction.
addInsertAfterStep Inserts the provided data after a specified node as an element, attribute, comment, text or processing instruction.
addInsertBeforeStep Inserts the provided data before a specified node.
addRemoveStep Removes a specified node.
addRenameStep Renames a specified node.
addUpdateStep Updates a specified node.
Table 3. XmlModify Class Methods

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). In the example you 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>" +
      "<title>XML in Databases </title>" +
       "<author>Ari Kaplan </author>" + 
       "</article>";

Add the journal element using the addInsertAfterStep() method.

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

The modification is 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);

A section attribute gets added to article element and a journal element gets added after the journal element in catalog1 document.

Replacing/Deleting XML Documents

Next, you update, rename, and delete elements in the catalog1 XML document. As an example, update the title "Using Bind Variables" to "Introduction to Bind Variables". Rename the "journal" element to "magazine". And, you delete the "journal" node added in the previous section. As the renaming of "journal" nodes is performed before the deletion of a "journal" node, the node that you actually delete is a "magazine" node. First, create XmlModify, XmlQueryContext, and XmlUpdateContext objects.

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

Select the title text node to be updated.

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

Specify the update content.Add a update modification to the XmlModify object using the addUpdateStep() method.

mod.addUpdateStep(select, updateContent);
String updateContent = 
"Introduction to Bind Variables";

Next, rename the "journal" nodes to "magazine". Select the "journal" nodes with an XQuery expression. Add a renaming modification to the XmlModify object using the addRenameStep() method.

mod.addRenameStep(select, "magazine");
select = xmlManager.prepare("/catalog/journal", qc);

Next, delete the "journal" node added in the previous section. As the "journal" nodes have been renamed to "magazine" delete a "magazine" node. Select the second "magazine" node in the catalog1 document.Add a remove modification to the XmlModify object using the addRemoveStep() method.

mod.addRemoveStep(select);
mod.addRenameStep(select, "magazine");

Obtain the catalog1 document from the BDB XML database and run the modifications on the document.

XmlDocument xmlDocument = 
xmlContainer.getDocument("catalog1");
XmlValue xmlValue = new XmlValue(xmlDocument);
 mod.execute(xmlValue, qc, uc);

A title text gets updated, "journal" node gets renamed to "magazine", and a 'magazine" node gets deleted. The BDB XML application used to access the BDB XML database is listed below.

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.XmlQueryContext;
import com.sleepycat.dbxml.XmlQueryExpression;
import com.sleepycat.dbxml.XmlResults;
import com.sleepycat.dbxml.XmlUpdateContext;
import com.sleepycat.dbxml.XmlValue;


public class BDBXML {
 XmlManager xmlManager = null;
 XmlContainer xmlContainer = null;

 public void createContainer() {
   try {
        xmlManager = new XmlManager();
        xmlManager.setDefaultContainerType
        (XmlContainer.NodeContainer);
        xmlContainer = xmlManager.createContainer
        ("catalog.dbxml");
        } catch (XmlException e) {
            System.out.println("XmlException" + 
            e.getMessage());
        } catch (java.io.FileNotFoundException e) {
            System.out.println("FileNotFoundException" 
            + e.getMessage());
        }
    }

    public void addDocument() {
        try {
            String docString =
                "<catalog title='Oracle Magazine' 
                publisher='Oracle Publishing'>" +
                "<journal date='March-April 2006'>" + 
                "<article>" +
                "<title>Using Bind Variables</title>" +
                "<author> Steve Muench </author>" + 
                "</article>" +
                "</journal>" + "</catalog>";

            String docName = "catalog1";

            XmlUpdateContext updateContext = 
               xmlManager.createUpdateContext();
            xmlContainer.putDocument(docName, 
               docString, updateContext, null);

      docString = "<catalog title='Oracle Magazine' 
      publisher='Oracle Publishing'>" +
                "<journal date='May-June 2006'>" + 
                "<article>" +
                "<title>From Application Express to XE
                 </title>" +
                "<author>David A. Kelly </author>" + 
       "</article>" +
                "</journal>" + "</catalog>";
            docName = "catalog2";
            xmlContainer.putDocument(docName, 
            docString, updateContext, null);
            XmlResults results = 
            xmlContainer.getAllDocuments(null);
            while (results.hasNext()) {
            XmlValue xmlValue = results.next();
            System.out.println(xmlValue.asString());
            }
        }
        catch (XmlException e) {
         System.out.println("XmlException" + 
            e.getMessage());
        }
    }

    public void queryDocument() {
        try {
            XmlQueryContext context = 
                   xmlManager.createQueryContext();

            String query = "collection
            ('catalog.dbxml')/catalog/journal/article
            /title/text()";
            XmlQueryExpression qe = xmlManager.prepare
            (query, context);
            XmlResults results = qe.execute(context);

            while (results.hasNext()) {
              XmlValue xmlValue = results.next();
              System.out.println(xmlValue.asString());
            }
        } catch (XmlException e) {
            System.out.println("XmlException" + 
            e.getMessage());
        }
    }

    public void modifyDocument() {
        try {
            XmlQueryContext qc = 
                   xmlManager.createQueryContext();
            XmlUpdateContext uc = 
                   xmlManager.createUpdateContext();
          XmlModify mod = xmlManager.createModify();
            XmlQueryExpression select = 
       xmlManager.prepare("/catalog/journal/article",
                    qc);
            mod.addAppendStep(select, 
                      XmlModify.Attribute, "section",
                "Developer");

            String objectContent = "<article>" +
                "<title>XML in Databases </title>" +
                "<author>Ari Kaplan </author>" + 
            "</article>";

            select = xmlManager.prepare("/catalog/
       journal[article/title='Using Bind Variables']",
                    qc);
       mod.addInsertAfterStep(select, 
       XmlModify.Element, "journal",objectContent);

            XmlDocument xmlDocument = 
                xmlContainer.getDocument("catalog1");
            XmlValue xmlValue = new XmlValue(xmlDocument);
            mod.execute(xmlValue, qc, uc);

       System.out.println("XML Documents after 
                                   modification");
       XmlResults results = 
                 xmlContainer.getAllDocuments(null);

            while (results.hasNext()) {
              xmlValue = results.next();
              System.out.println(xmlValue.asString());
            }
        } catch (XmlException e) {
           System.out.println("XmlException" + 
            e.getMessage());
        }
    }

    public void updateDocument() {
        try {
            XmlQueryContext qc = 
                   xmlManager.createQueryContext();
            XmlUpdateContext uc = 
                  xmlManager.createUpdateContext();
            XmlModify mod = xmlManager.createModify();

            String updateContent = "Introduction to 
            Bind Variables";
            XmlQueryExpression select = 
            xmlManager.prepare("/catalog/journal
            /article[title='Using             
              Bind Variables']/title/text()",qc);
            mod.addUpdateStep(select, updateContent);

            select = xmlManager.prepare("/catalog/
            journal", qc);
            mod.addRenameStep(select, "magazine");

            select = xmlManager.prepare("/catalog/
            magazine[2]", qc);
            mod.addRemoveStep(select);

            System.out.println("XML Documents 
            after Update");

            XmlDocument xmlDocument = 
            xmlContainer.getDocument("catalog1");
            XmlValue xmlValue = 
                         new XmlValue(xmlDocument);
            mod.execute(xmlValue, qc, uc);

            XmlResults results = 
                 xmlContainer.getAllDocuments(null);

            while (results.hasNext()) {
                xmlValue = results.next();
                System.out.println(xmlValue.asString());
            }
        } catch (XmlException e) {
            System.out.println("XmlException" + 
            e.getMessage());
        }
    }

    public static void main(String[] argv) {
        BDBXML bdbXML = new BDBXML();

        bdbXML.createContainer();
        bdbXML.addDocument();
        bdbXML.queryDocument();
        bdbXML.modifyDocument();
        bdbXML.updateDocument();
    }
}

The output from BDBXML.java is shown below.

<catalog title="Oracle Magazine" 
publisher="Oracle Publishing"><journal 
date="March-April 2006"><article>
<title>Using Bind Variables</title>
<author>Steve Muench </author></article></journal>
</catalog><catalog title="Oracle Magazine" 
publisher="Oracle Publishing"><journal 
date="May-June 2006"><article><title>
From Application Express to XE</title>
<author>David A. Kelly </author></article></journal>
</catalog>

Using Bind Variables
From Application Express to XE

XML Documents after modification

<catalog title="Oracle Magazine" publisher=
"Oracle Publishing"><journal date=
"March-April 2006"><article section="Developer">
<title>Using Bind Variables</title>
<author> Steve Muench </author>
</article></journal>
<journal><article><title>
XML in Databases</title><author>Ari
Kaplan</author></article></journal>
</catalog><catalog title="Oracle Magazine" 
publisher="Oracle Publishing"><journal date="May-June
2006"><article><title>From Application 
Express to XE</title><author>David A. Kelly 
</author></article>
</journal></catalog>

XML Documents after Update

<catalog title="Oracle Magazine" publisher=
"Oracle Publishing"><magazine date=
"March-April 2006"><article section=
"Developer"><title>Introduction to Bind 
Variables</title><author> Steve Muench </author>
</article></magazine>
</catalog>
<catalog title="Oracle Magazine" publisher=
"Oracle Publishing"><journal date="May-June 2006"
><article><title>From Application Express to XE
</title><author>
David A. Kelly </author></article></journal>
</catalog>

Conclusion

A relational database has its limitations when it comes to storing XML documents. The Berkeley DB XML database stores complete XML documents and provides the provision to query, modify and update documents without having to retrieve the documents from the database.

Oracle acquired Berkeley DB from Sleepycat Software in February 2006 and overhauled the database. Oracle Berkeley DB XML Release 2.4 supports the Candiadate Recommendation of W3C's XQuery Update 1.0.

References

Deepak Vohra (dvohra09@yahoo.com) is a Oracle Certified Associate, Sun Certified Java Programmer and Web Component Developer. Deepak is the author of the book Ajax in Oracle JDeveloper.



1 to 2 of 2
  1. Los Angeles Locksmith 323-678-2704
    2010-06-16 12:25:12 carpetcare
  2. Locksmiths in Los Angeles CA 1-323-678-2704
    2009-07-02 14:46:21 carpetcare
1 to 2 of 2