An Introduction to XML Data Binding in C++

Wednesday, August 22, 2007

XML processing has become a common task that many C++ application developers have to deal with. Using low-level XML access APIs such as DOM and SAX is tedious and error-prone, especially for large XML vocabularies. XML Data Binding is a new alternative which automates much of the task by presenting the information stored in XML as a statically-typed, vocabulary-specific object model. This article introduces XML Data Binding and shows how it can simplify XML processing in C++.

Introduction

A typical C++ application that has to manipulate the data stored in XML format uses one of the two common XML access APIs: Document Object Model (DOM) or Simple API for XML (SAX). DOM represents XML as a tree-like data structure which can be navigated and examined by the application. SAX is an event-driven XML processing API. The application registers its interest in certain events, such as start element tag, attribute, or text, which are then triggered during the parsing of an XML instance. While DOM has to read the whole document into memory before the application can examine the data, SAX delivers the data as parsing progresses.

Anyone who has had to handle a large XML vocabulary using DOM or SAX can attest that the task is hardly enjoyable. After all, both DOM and SAX are raw representations of the XML structure, operating in generic elements, attributes, and text. An application developer often has to write a substantial amount of bridging code that identifies and transforms pieces of information encoded in XML to a representation more suitable for consumption by the application logic. Consider, for example, a simple XML document that describes a person:

<person>
<name>John Doe</name>
<gender>male</gender>
<age>32</age>
</person>

If we wanted to make sure the person’s age is greater than some predefined limit, with both DOM and SAX we would first have to find the age element and then parse the string representation of 32 to obtain the integer value that can be compared. Another significant drawback of generic APIs is string-based flow control. In the example above, when we search for the age element we pass the element name as a string. If we misspell it, we (or a user of our program) will most likely only discover this bug at runtime. String-based flow control also reduces code readability and maintainability. Furthermore, generic APIs lack type safety because all the information is represented as text. For example, we can compare the content of the gender element to an invalid value without any warning from the compiler:

DOMElement* gender = …

if (gender->getTextContent () == “man”)
{

}

In recent years a new approach to XML processing, called XML Data Binding, has emerged thanks to the progress in XML vocabulary specification languages (XML schemas). The main idea of XML Data Binding is to skip the raw representation of XML and instead deliver the data in an object-oriented representation that models a particular vocabulary. As a result, the application developer does not have to produce the bridging code anymore because the object model can be used directly in the implementation of the application logic. In the example above, instead of searching for the age element and then manually converting the text to an integer, we would simply call the age() function on the person object that already returns the age as an integer. The name XML Data Binding comes from the observation that the object representation is essentially bound to and becomes a proxy for the data stored in XML.

The vocabulary-specific object representation along with other support code such as parsing and serialization functions are generated by a data binding compiler from an XML schema. A schema is a formal specification of a vocabulary that defines the names of elements and attributes, their content, and the structural relationship between them. The majority of XML Data Binding tools use the W3C XML Schema specification language due to its object-oriented approach to the vocabulary description as well as its widespread use. The following fragment describes the XML vocabulary presented above using W3C XML Schema:

<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”>

<xs:simpleType name=”gender_t”>
<xs:restriction base=”xs:string”>
<xs:enumeration value=”male”/>
<xs:enumeration value=”female”/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name=”person_t”>
<xs:sequence>
<xs:element name=”name” type=”xs:string”/>
<xs:element name=”gender” type=”gender_t”/>
<xs:element name=”age” type=”xs:short”/>
</xs:sequence>
</xs:complexType>

<xs:element name=”person” type=”person_t”/>

</xs:schema>

Even if you are not familiar with XML Schema, it should be fairly straightforward to figure out what is going on here. The gender_t type is an enumeration with the only valid string values being “male” and “female”. The person_t type is defined as a sequence of the nested name, gender, and age elements. Note that the term sequence in XML Schema means that elements should appear in a particular order as opposed to appearing multiple times. Finally, the globally-defined person element prescribes the root element for our vocabulary. For an easily-approachable introduction to XML Schema refer to XML Schema Part 0: Primer.

Similar to the direct XML representation APIs, XML Data Binding supports both in-memory and event-driven programming models. In the next sections we will examine the complexity of performing common XML processing tasks using DOM and SAX compared to in-memory and event-driven XML Data Binding. The DOM and SAX examples in this article are based on Apache Xerces-C++ open-source XML parser with character conversions omitted to keep the code focused. The XML Data Binding examples are based on CodeSynthesis XSD open-source XML Schema to C++ data binding compiler.

Source : Artima



 
Indelv.com is for sale!
 
ERP systemen
Alle ERP-systemen op een rij, compleet met ERP-nieuws en ERP-software informatie.
www.ERPcentraal.nl
ERP systemen
Alle ERP-systemen op een rij.
www.erpmatrix.nl


Quick Links
Our Friends
Cool Places
Visit also
About Us