blob: 435a4cf01af5e20a0194c3c62b4385dbd4f25c66 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
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:
protocol.xsd
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.
balance.xml
withdraw.xml
deposit.xml
Sample XML instances for the protocol requests.
protocol.hxx
protocol.cxx
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
option.
dom-parse.hxx
dom-parse.cxx
Definition and implementation of the parse() function that
parses an XML document to a DOM document.
dom-serialize.hxx
dom-serialize.cxx
Definition and implementation of the serialize() function that
serializes a DOM document to XML.
driver.cxx
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
execute:
$ ./driver balance.xml
$ ./driver withdraw.xml
$ ./driver deposit.xml
|