The Berkeley DB XML API

The Berkeley DB XML command line shell is a tool and not an end-user application, it has been useful in exploring the features of this system. Applications will be built using the programming language APIs. In this final example, we implement our first example, the phonebook example, in C++.

#include <string>
#include <fstream>
#include "dbxml/DbXml.hpp"

using namespace std;
using namespace DbXml;

int
main(int argc, char **argv)
{
    try {
        XmlManager mgr;
        
        // Create the phonebook container
        XmlContainer cont = mgr.createContainer("phone.dbxml");
        
        // Add the phonebook entries to the container
        XmlUpdateContext uc = mgr.createUpdateContext();
        cont.putDocument("phone1", "<phonebook><name><first>Tom</first>
<last>Jones</last></name><phone type=\"home\">420-203-2032</phone>
</phonebook>", uc);
        cont.putDocument("phone2", "<phonebook><name><first>Lisa</first>
<last>Smith</last></name><phone type=\"home\">420-992-4801</phone>
<phone type=\"cell\">390-812-4292</phone></phonebook>", 
uc);

        // Run an XQuery against the phonebook container
        XmlQueryContext qc = mgr.createQueryContext();        
        XmlResults res = 
        mgr.query("collection('phone.dbxml')/phonebook[name/first = 'Lisa']/
phone[@type = 'home']/string()", qc);

        // Print out the result of the query
        XmlValue value;
        while (res.next(value))
            cout << "Value: " << value.asString() << endl;
    } catch (XmlException &e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }
    return 0;
}

While this example is in C++, the BDB XML API is similar across all supported languages. This makes it easy to transfer knowledge about the API between languages and can enable useful scenarios such as prototyping the application in Python and then implementing the final version in Java or C++. Because of the similarity across languages porting, the BDB XML code is relatively simple.