blob: 410784da8d618d8fad32541149ebdc34ae8c5b95 (
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
59
60
61
62
63
64
65
66
67
68
|
This example shows how to handle XML vocabularies with multiple
root elements using the C++/Parser mapping.
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.
balance.xml
withdraw.xml
deposit.xml
Sample XML instances for the protocol requests.
protocol.hxx
C++ types that describe the protocol requests. These are
hand-written.
protocol.map
Type map. It maps XML Schema types defined in protocol.xsd
to the C++ types defined in protocol.hxx.
protocol-pskel.hxx
protocol-pskel.cxx
Parser skeletons generated by the XSD compiler from protocol.xsd and
protocol.map using the following command line:
xsd cxx-parser --type-map=protocol.map protocol.xsd
Or if using Expat instead of Xerces-C++ as the underlying XML parser:
xsd cxx-parser --xml-parser=expat --type-map=protocol.map protocol.xsd
protocol-pimpl.hxx
protocol-pimpl.cxx
Parser implementations that construct the custom object
model from an XML instance using the types from protocol.hxx.
These are hand-written implementations of the parser skeletons
defined in protocol-pskel.hxx.
driver.cxx
Driver for the example. It implements a custom document parser
that determines which request is being parsed and uses the
corresponding parser implementation. The document parser
intentionally does not support the deposit request to show
how to handle unknown documents. The driver first constructs
a parser instance from all the individual parsers found in
protocol-pimpl.hxx. In then invokes this parser instance to
parse the input file and produce the in-memory object model.
Finally, it prints the contents of the object model to STDERR.
To compile and link the example manually from the command line we can use
the following commands (replace 'c++' with your C++ compiler name):
c++ -c protocol-pskel.cxx
c++ -c protocol-pimpl.cxx
c++ -c driver.cxx
c++ -o driver driver.o protocol-pskel.o protocol-pimpl.o -lxerces-c
Or if using Expat as the underlying XML parser:
c++ -o driver driver.o protocol-pskel.o protocol-pimpl.o -lexpat
To run the example on the sample XML request documents execute:
./driver balance.xml
./driver withdraw.xml
./driver deposit.xml
|