blob: 435a4cf01af5e20a0194c3c62b4385dbd4f25c66 (plain
This example shows how to handle XML vocabularies with multiple
root elements using the element type and element map features
of the C++/Tree mapping. The main purpose of element types is
to distinguish object models with the same root type but with
different root elements. The element map allows uniform parsing
and serialization of multiple root elements.
The example consists of the following files:
XML Schema which defines a simple bank account protocol with
requests such as withdraw and deposit. Note that some request
and response elements are of the same type.
Sample XML instances for the protocol requests.
C++ types that represent the given vocabulary. These are
generated by the XSD compiler from protocol.xsd. Generation of
element types instead of parsing and serialization functions is
requested with the --generate-element-type option. Generation of
the element map is requested with the --generate-element-map
Definition and implementation of the parse() function that
parses an XML document to a DOM document.
Definition and implementation of the serialize() function that
serializes a DOM document to XML.
Driver for the example. It first calls the above-mentioned parse()
function to parse the input file to a DOM document. It then calls
the parse() function on the element map to parse the root document
element to the object model. The object model is returned as a
pointer to xml_schema::element_type which is a common base type for
all element types. The driver then determines which request it has
received either using RTTI or by comparing the root element names.
Once the request type is determined, information about it is printed
to STDERR and the corresponding response is created. Finally, the
driver serializes the opaque response object to a DOM document
using the element map and then serializes this DOM document to
STDOUT using the above-mentioned serialize() function.
To run the example on the sample XML request documents simply
$ ./driver balance.xml
$ ./driver withdraw.xml
$ ./driver deposit.xml