2 C and C++ XML Data Bindings {#mainpage}
3 ===========================
10 This article presents a detailed overview of the gSOAP XML data bindings for C
11 and C++. The XML data bindings for C and C++ are extensively used with gSOAP
12 Web services to serialize C and C++ data in XML as part of the SOAP/XML Web
13 services payloads. Also REST XML with gSOAP relies on XML serialization of C
14 and C++ data via XML data bindings.
16 The major advantage of XML data bindings is that your application data is
17 always **type safe** in C and C++ by binding XML schema types to C/C++ types.
18 So integers in XML are bound to C integers, strings in XML are bound to C or
19 C++ strings, complex types in XML are bound to C structs or C++ classes, and so
20 on. The structured data you create and accept will fit the data model and is
21 **static type safe**. In other words, by leveraging strong typing in C/C++,
22 your XML data meets **XML schema validation requirements** and satisfies **XML
23 interoperability requirements**.
25 In fact, gSOAP data bindings are more powerful than simply representing C/C++
26 data in XML. The gSOAP tools implement true and tested **structure-preserving
27 serialization** of C/C++ data in XML, including the serialization of cyclic
28 graph structures with id-ref XML attributes. The gSOAP tools also generate
29 routines for deep copying and deep deletion of C/C++ data structures to
30 simplify memory management. In addition, C/C++ structures are deserialized
31 into managed memory, managed by the gSOAP `soap` context.
33 At the end of this article two examples are given to illustrate the application
34 of XML data bindings. The first simple example <i>`address.cpp`</i> shows how to use
35 wsdl2h to bind an XML schema to C++. The C++ application reads and writes an
36 XML file into and from a C++ "address book" data structure as a simple example.
37 The C++ data structure is an STL vector of address objects. The second example
38 <i>`graph.cpp`</i> shows how C++ data can be accurately serialized as a tree, digraph,
39 and cyclic graph in XML. The digraph and cyclic graph serialization rules
40 implement SOAP 1.1/1.2 multi-ref encoding with id-ref attributes to link
41 elements through IDREF XML references, creating a an XML graph with pointers to
42 XML nodes that preserves the structural integrity of the serialized C++ data.
44 These examples demonstrate XML data bindings only for relatively simple data
45 structures and types. The gSOAP tools support more than just these type of
46 structures to serialize in XML. There are practically no limits to the
47 serialization of C and C++ data types in XML.
49 Also the support for XML schema (XSD) components is unlimited. The wsdl2h tool
50 maps schemas to C and C++ using built-in intuitive mapping rules, while
51 allowing the mappings to be customized using a <i>`typemap.dat`</i> file with mapping
52 instructions for wsdl2h.
54 The information in this article is applicable to gSOAP 2.8.26 and greater that
55 support C++11 features. However, C++11 is not required. The material and the
56 examples in this article use plain C and C++, until the point where we
57 introduce C++11 smart pointers and scoped enumerations. While most of the
58 examples in this article are given in C++, the concepts also apply to C with
59 the exception of containers, smart pointers, classes and their methods. None
60 of these exceptions limit the use of the gSOAP tools for C in any way.
62 The data binding concepts described in this article were first envisioned in
63 1999 by Prof. Robert van Engelen at the Florida State University. An
64 implementation was created in 2000, named "stub/skeleton compiler". The first
65 articles on its successor version "gSOAP" appeared in 2002. The principle of
66 mapping XSD components to C/C++ types and vice versa is now widely adopted in
67 systems and programming languages, including Java web services and by C# WCF.
69 We continue to be committed to our goal to empower C/C++ developers with
70 powerful autocoding tools for XML. Our commitment started in the very early
71 days of SOAP by actively participating in
72 [SOAP interoperability testing](http://www.whitemesa.com/interop.htm),
73 participating in the development and testing of the
74 [W3C XML Schema Patterns for Databinding Interoperability](http://www.w3.org/2002/ws/databinding),
75 and continues by contributing to the development of
76 [OASIS open standards](https://www.oasis-open.org) in partnership with leading
77 IT companies in the world.
79 🔝 [Back to table of contents](#)
81 Notational Conventions {#conventions}
82 ======================
84 The typographical conventions used by this document are:
86 * `Courier` denotes C and C++ source code.
88 * <i>`Courier`</i> denotes XML content, JSON content, file and path names, and URIs.
90 * <b>`Courier`</b> denotes HTTP content, text file content, and shell commands with command line options and arguments.
92 The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
93 "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to
94 be interpreted as described in RFC-2119.
96 🔝 [Back to table of contents](#)
98 Mapping WSDL and XML schemas to C/C++ {#tocpp}
99 =====================================
101 To convert WSDL and XML schemas (XSD files) to code, we use the wsdl2h command
102 on the command line (or command prompt), after opening a terminal. The wsdl2h
103 command generates the data binding interface code that is saved to a special
104 Web services and data bindings interface header file with extension <i>`.h`</i>
105 that contains the WSDL service declarations and the data binding interface
106 declarations in a familiar C/C++ format:
108 wsdl2h [options] -o file.h ... XSD and WSDL files ...
110 This command converts WSDL and XSD files to C++ (or pure C with
111 <b>`wsdl2h -c`</b>) and saves the data binding interface to a interface header
112 file <i>`file.h`</i> that uses familiar C/C++ syntax extended with `//gsoap`
113 [directives](#directives) and annotations. Notational conventions are used in
114 the data binding interface to declare serializable C/C++ types and functions
115 for Web service operations.
117 The WSDL 1.1/2.0, SOAP 1.1/1.2, and XSD 1.0/1.1 standards are supported by the
118 gSOAP tools. In addition, the most popular WS specifications are also
119 supported, including WS-Addressing, WS-ReliableMessaging, WS-Discovery,
120 WS-Security, WS-Policy, WS-SecurityPolicy, and WS-SecureConversation.
122 This article focusses mainly on XML data bindings. XML data bindings for C/C++
123 bind XML schema types to C/C++ types. So integers in XML are bound to C
124 integers, strings in XML are bound to C or C++ strings, complex types in XML
125 are bound to C structs or C++ classes, and so on.
127 A data binding is dual, meaning supporting a two way direction for development.
128 Either you start with WSDLs and/or XML schemas that are mapped to equivalent
129 C/C++ types, or you start with C/C++ types that are mapped to XSD types.
130 Either way, the end result is that you can serialize C/C++ types in XML such
131 that your XML is an instance of XML schema(s) and is validated against these
134 This covers all of the following standard XSD components with their optional
135 attributes and properties:
137 XSD component | attributes and properties
138 -------------- | -------------------------
139 schema | targetNamespace, version, elementFormDefault, attributeFormDefault, defaultAttributes
140 attribute | name, ref, type, use, default, fixed, form, targetNamespace, wsdl:arrayType
141 element | name, ref, type, default, fixed, form, nillable, abstract, substitutionGroup, minOccurs, maxOccurs, targetNamespace
143 complexType | name, abstract, mixed, defaultAttributesApply
145 choice | minOccurs, maxOccurs
146 sequence | minOccurs, maxOccurs
147 group | name, ref, minOccurs, maxOccurs
148 attributeGroup | name, ref
149 any | minOccurs, maxOccurs
152 And also the following standard XSD directives are covered:
154 directive | description
155 ---------- | -----------
156 import | Imports a schema into the importing schema for referencing
157 include | Include schema component definitions into a schema
158 override | Override by replacing schema component definitions
159 redefine | Extend or restrict schema component definitions
160 annotation | Annotates a component
162 The XSD facets and their mappings to C/C++ are:
165 -------------- | -------
167 simpleContent | class/struct wrapper with `__item` member
168 complexContent | class/struct
169 list | `enum*` bitmask (`enum*` enumerates a bitmask up to 64 bits)
170 extension | class/struct inheritance/extension
171 restriction | `typedef` and class/struct inheritance/redeclaration
172 length | `typedef` with restricted content length annotation
173 minLength | `typedef` with restricted content length annotation
174 maxLength | `typedef` with restricted content length annotation
175 minInclusive | `typedef` with numerical value range restriction annotation
176 maxInclusive | `typedef` with numerical value range restriction annotation
177 minExclusive | `typedef` with numerical value range restriction annotation
178 maxExclusive | `typedef` with numerical value range restriction annotation
179 precision | `typedef` with pattern annotation (pattern used for output, but input is not validated)
180 scale | `typedef` with pattern annotation (pattern used for output, but input is not validated)
181 totalDigits | `typedef` with pattern annotation (pattern used for output, but input is not validated)
182 fractionDigits | `typedef` with pattern annotation (pattern used for output, but input is not validated)
183 pattern | `typedef` with pattern annotation (define `soap::fsvalidate` callback to validate patterns)
184 union | string with union of value
186 All primitive XSD types are supported, including but not limited to the
190 ---------------- | -------
191 any/anyType | `_XML` string with literal XML content (or enable DOM with wsdl2h option `-d`)
192 anyURI | string (i.e. `char*`, `wchar_t*`, `std::string`, `std::wstring`)
193 string | string (i.e. `char*`, `wchar_t*`, `std::string`, `std::wstring`)
194 boolean | `bool` (C++) or `enum xsd__boolean` (C)
195 byte | `char` (i.e. `int8_t`)
196 short | `short` (i.e. `int16_t`)
197 int | `int` (i.e. `int32_t`)
198 long | `LONG64` (i.e. `long long` and `int64_t`)
199 unsignedByte | `unsigned char` (i.e. `uint8_t`)
200 unsignedShort | `unsigned short` (i.e. `uint16_t`)
201 unsignedInt | `unsigned int` (i.e. `uint32_t`)
202 unsignedLong | `ULONG64` (i.e. `unsigned long long` and `uint64_t`)
205 integer | string or `#import "custom/int128.h"` to use 128 bit `xsd__integer`
206 decimal | string or `#import "custom/long_double.h"` to use `long double`
207 precisionDecimal | string
208 duration | string or `#import "custom/duration.h"` to use 64 bit `xsd__duration`
209 dateTime | `time_t` or `#import "custom/struct_tm.h"` to use `struct tm` for `xsd__dateTime`
210 time | string or `#import "custom/long_time.h"` to use 64 bit `xsd__time`
211 date | string or `#import "custom/struct_tm_date.h"` to use `struct tm` for `xsd__date`
212 hexBinary | special class/struct `xsd__hexBinary`
213 base64Binary | special class/struct `xsd__base64Binary`
214 QName | `_QName` string (URI normalization rules are applied)
216 All other primitive XSD types not listed above are mapped to strings, by
217 wsdl2h generating a `typedef` to string for these types. For example,
218 <i>`xsd:token`</i> is bound to a C++ or C string:
221 typedef std::string xsd__token; // C++
222 typedef char *xsd__token; // C (wsdl2h option -c)
225 This associates a compatible value space to the type with the appropriate XSD
226 type name used by the soapcpp2-generated serializers.
228 It is possible to remap types by adding the appropriate mapping rules to
229 <i>`typemap.dat`</i> as we will explain in more detail in the next section.
231 Imported custom serializers are intended to extend the C/C++ type bindings when
232 the default binding to string is not satisfactory to your taste and if the
233 target platform supports these C/C++ types. To add custom serializers to
234 <i>`typemap.dat`</i> for wsdl2h, see [adding custom serializers](#custom) below.
236 🔝 [Back to table of contents](#)
238 Using typemap.dat to customize data bindings {#typemap}
239 ============================================
241 Use a <i>`typemap.dat`</i> file to redefine namespace prefixes and to customize type
242 bindings for the the generated header files produced by the wsdl2h tool. The
243 <i>`typemap.dat`</i> is the default file processed by wsdl2h. Use <b>`wsdl2h -tfile.dat`</b>
244 option <b>`-tfile.dat`</b> to specify a different mapping file <i>`file.dat`</i>.
246 Declarations in <i>`typemap.dat`</i> can be broken up over multiple lines by
247 continuing on the next line by ending each line to be continued with a
248 backslash <b>`\`</b>. The limit is 4095 characters per line, whether the line is
251 🔝 [Back to table of contents](#)
253 XML namespace bindings {#typemap1}
254 ----------------------
256 The wsdl2h tool generates C/C++ type declarations that use `ns1`, `ns2`, etc.
257 as schema-binding URI prefixes. These default prefixes are generated somewhat
258 arbitrarily for each schema targetNamespace URI, meaning that their ordering
259 may change depending on the WSDL and XSD order of processing with wsdl2h.
261 Therefore, it is **strongly recommended** to declare your own prefix for each
262 schema URI in <i>`typemap.dat`</i> to reduce maintaince effort of your code. This
263 is more robust when anticipating possible changes of the schema(s) and/or the
264 binding URI(s) and/or the tooling algorithms.
266 The first and foremost important thing to do is to define prefix-URI bindings
267 for our C/C++ code by adding the following line(s) to our <i>`typemap.dat`</i> or make
268 a copy of this file and add the line(s) that bind our choice of prefix name to
273 For example, to use `g` as a prefix for the "urn:graph" XML namespace:
277 This produces `g__name` C/C++ type names that are bound to the "urn:graph"
278 schema by association of `g` to the generated C/C++ types.
280 This means that <i>`<g:name xmlns:g="urn:graph">`</i> is parsed as an instance of a
281 `g__name` C/C++ type. Also <i>`<x:name xmlns:x="urn:graph">`</i> parses as an
282 instance of `g__name`, because the prefix <i>`x`</i> has the same URI value
283 <i>`urn:graph`</i>. Prefixes in XML have local scopes (like variables in a block).
285 The first run of wsdl2h will reveal the XML namespace URIs, so you do not need
286 to search WSDLs and XSD files for all of the target namespaces. Just copy them
287 from the generated header file after the first run into <i>`typemap.dat`</i> for
290 @note Only define a namespace prefix once in <i>`typemap.dat`</i>. That is, do not
291 use the same prefix for multiple XML namespace URIs. This is to avoid
292 namespace conflicts that may cause failed builds and failures in XML parsing
293 and XML schema validation.
295 🔝 [Back to table of contents](#)
297 XSD type bindings {#typemap2}
300 Custom C/C++ type bindings can be declared in <i>`typemap.dat`</i> to associate C/C++
301 types with specific schema types. These type bindings have four parts:
303 prefix__type = declaration | use | ptruse
307 - <b>`prefix__type`</b> is the schema type to be customized (the <b>`prefix__type`</b> name
308 uses the common double underscore naming convention);
310 - <b>`declaration`</b> declares the C/C++ type in the wsdl2h-generated header file.
311 This part can be empty if no explicit declaration is needed;
313 - <b>`use`</b> is an optional part that specifies how the C/C++ type is used in the
314 code. When omitted, it is the same as <b>`prefix__type`</b>;
316 - <b>`ptruse`</b> is an optional part that specifies how the type is used as a pointer
317 type. By default it is the <b>`use`</b> type name with a <b>`*`</b> or C++11
318 <b>`std::shared_ptr<type>`</b> when enabled (see further below). If <b>`use`</b> is already a
319 pointer type by the presence of a <b>`*`</b> in the <b>`use`</b> part, then the default
320 <b>`ptruse`</b> type is the same as the <b>`use`</b> type (that is, no double
321 pointers <b>`**`</b> will be created in this case).
323 For example, to map <i>`xsd:duration`</i> to a `long long` (`LONG64`) type that holds
324 millisecond duration values, we can use the custom serializer declared in
325 <i>`gsoap/custom/duration.h`</i> by adding the following line to <i>`typemap.dat`</i>:
327 xsd__duration = #import "custom/duration.h"
329 Here, we omitted the second and third parts, because `xsd__duration` is the
330 name that wsdl2h uses for this type in our generated code so we should leave
331 the <b>`use`</b> part unspecified. The third part is omitted to let wsdl2h use
332 `xsd__duration *` for pointers or `std::shared_ptr<xsd__duration>` if smart
333 pointers are enabled.
335 To map <i>`xsd:string`</i> to `wchar_t*` wide strings:
337 xsd__string = | wchar_t* | wchar_t*
339 Note that the first part is empty, because `wchar_t` is a C type and does not
340 need to be declared. A <b>`ptruse`</b> part is also defined in this example, but does
341 not need to be because the <b>`use`</b> part `wchar_t*` is already a pointer.
343 When the auto-generated declaration should be preserved but the <b>`use`</b> or
344 <b>`ptruse`</b> parts replaced, then we use an ellipsis for the declaration part:
346 prefix__type = ... | use | ptruse
348 This is useful to map schema polymorphic types to C types for example, where we
349 need to be able to both handle a base type and its extensions as per schema
350 extensibility. Say we have a base type called <i>`ns:base`</i> that is extended, then
351 we can remap this to a C type that permits referening the extended types via a
354 ns__base = ... | int __type_base; void*
356 such that `__type_base` and `void*` will be used to (de)serialize any data
357 type, including base and its derived types. The `__type_base` integer is set
358 to a `SOAP_TYPE_T` value to indicate what type of data the `void*` pointer
361 🔝 [Back to table of contents](#)
363 Custom serializers for XSD types {#custom}
364 --------------------------------
366 In the previous part we saw how a custom serializer is used to bind
367 <i>`xsd:duration`</i> to a `long long` (`LONG64` or `int64_t`) type to store millisecond
370 xsd__duration = #import "custom/duration.h"
372 The `xsd__duration` type is an alias of `long long` (`LONG64` or `int64_t`).
374 While wsdl2h will use this binding declared in <i>`typemap.dat`</i>
375 automatically, you will also need to compile <i>`gsoap/custom/duration.c`</i>.
376 Each custom serializer has an interface header file to be imported into another
377 interface header file that declares the custom type for soapcpp2 and a
378 serializer implementation file written in C, which should be compiled with the
379 application. You can compile these in C++ (rename files to <i>`.cpp`</i> if
382 We will discuss the custom serializers that are available to you.
384 🔝 [Back to table of contents](#)
386 ### xsd:integer {#custom-1}
388 The wsdl2h tool maps <i>`xsd:integer`</i> to a string by default. To map <i>`xsd:integer`</i> to
389 the 128 bit big int type `__int128_t`:
391 xsd__integer = #import "custom/int128.h"
393 The `xsd__integer` type is an alias of `__int128_t`.
395 @warning Beware that the <i>`xsd:integer`</i> value space of integers is in principle
396 unbounded and values can be of arbitrary length. A value range fault
397 `SOAP_TYPE` (value exceeds native representation) or `SOAP_LENGTH` (value
398 exceeds range bounds) will be thrown by the deserializer if the value is out of
401 Other XSD integer types that are restrictions of <i>`xsd:integer`</i>, are
402 <i>`xsd:nonNegativeInteger`</i> and <i>`xsd:nonPositiveInteger`</i>, which are further restricted
403 by <i>`xsd:positiveInteger`</i> and <i>`xsd:negativeInteger`</i>. To bind these types to
404 `__int128_t` add the following definitions to <i>`typemap.dat`</i>:
406 xsd__nonNegativeInteger = typedef xsd__integer xsd__nonNegativeInteger 0 : ;
407 xsd__nonPositiveInteger = typedef xsd__integer xsd__nonPositiveInteger : 0 ;
408 xsd__positiveInteger = typedef xsd__integer xsd__positiveInteger 1 : ;
409 xsd__negativeInteger = typedef xsd__integer xsd__negativeInteger : -1 ;
411 Or simply uncomment these definitions in <i>`typemap.dat`</i> when you are using the
412 latest gSOAP releases.
414 @note If `__int128_t` 128 bit integers are not supported on your platform and if it
415 is certain that <i>`xsd:integer`</i> values are within 64 bit value bounds for your
416 application's use, then you can map this type to `LONG64`:
418 xsd__integer = typedef LONG64 xsd__integer;
420 @note Again, a value range fault `SOAP_TYPE` or `SOAP_LENGTH` will be thrown by
421 the deserializer if the value is out of range.
423 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/int128.c`</i> with your project.
425 @see Section [numerical types](#toxsd5).
427 🔝 [Back to table of contents](#)
429 ### xsd:decimal {#custom-2}
431 The wsdl2h tool maps <i>`xsd:decimal`</i> to a string by default. To map <i>`xsd:decimal`</i> to
432 extended precision floating point:
434 xsd__decimal = #import "custom/long_double.h" | long double
436 By contrast to all other custom serializers, this serializer enables `long
437 double` natively without requiring a new binding name (`xsd__decimal` is NOT
440 If your system supports <i>`quadmath.h`</i> quadruple precision floating point
441 `__float128`, you can map <i>`xsd:decimal`</i> to `xsd__decimal` that is an alias of
444 xsd__decimal = #import "custom/float128.h"
446 @warning Beware that <i>`xsd:decimal`</i> is in principle a decimal value with arbitraty
447 lengths. A value range fault `SOAP_TYPE` will be thrown by the deserializer if
448 the value is out of range.
450 In the XML payload the special values <i>`INF`</i>, <i>`-INF`</i>, <i>`NaN`</i>
451 represent plus or minus infinity and not-a-number, respectively.
453 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/long_double.c`</i> with your
456 @see Section [numerical types](#toxsd5).
458 🔝 [Back to table of contents](#)
460 ### xsd:dateTime {#custom-3}
462 The wsdl2h tool maps <i>`xsd:dateTime`</i> to `time_t` by default.
464 The trouble with `time_t` when represented as 32 bit `long` integers is that it
465 is limited to dates between 1970 and 2038. A 64 bit `time_t` is safe to use if
466 the target platform supports it, but lack of 64 bit `time_t` portability may
467 still cause date range issues.
469 For this reason `struct tm` should be used to represent wider date ranges. This
470 custom serializer avoids using date and time information in `time_t`. You get
471 the raw date and time information. You only lose the day of the week
472 information. It is always Sunday (`tm_wday=0`).
474 To map <i>`xsd:dateTime`</i> to `xsd__dateTime` which is an alias of `struct tm`:
476 xsd__dateTime = #import "custom/struct_tm.h"
478 If the limited date range of `time_t` is not a problem but you want to increase
479 the time precision with fractional seconds, then we suggest to map <i>`xsd:dateTime`</i>
482 xsd__dateTime = #import "custom/struct_timeval.h"
484 If the limited date range of `time_t` is not a problem but you want to use the
485 C++11 time point type `std::chrono::system_clock::time_point` (which internally
488 xsd__dateTime = #import "custom/chrono_time_point.h"
490 Again, we should make sure that the dates will not exceed the date range when
491 using the default `time_t` binding for <i>`xsd:dateTime`</i> or when binding
492 <i>`xsd:dateTime`</i> to `struct timeval` or to `std::chrono::system_clock::time_point`.
493 These are safe to use in applications that use <i>`xsd:dateTime`</i> to record date
494 stamps within a given window. Otherwise, we recommend the `struct tm` custom
497 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/struct_tm.c`</i> with your
500 You could even map <i>`xsd:dateTime`</i> to a plain string (use `char*` with C and
501 `std::string` with C++). For example:
503 xsd__dateTime = | char*
505 @see Section [date and time types](#toxsd7).
507 🔝 [Back to table of contents](#)
509 ### xsd:date {#custom-4}
511 The wsdl2h tool maps <i>`xsd:date`</i> to a string by default. We can map <i>`xsd:date`</i> to
514 xsd__date = #import "custom/struct_tm_date.h"
516 The `xsd__date` type is an alias of `struct tm`. The serializer ignores the
517 time part and the deserializer only populates the date part of the struct,
518 setting the time to 00:00:00. There is no unreasonable limit on the date range
519 because the year field is stored as an integer (`int`).
521 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/struct_tm_date.c`</i> with your
524 @see Section [date and time types](#toxsd7).
526 🔝 [Back to table of contents](#)
528 ### xsd:time {#custom-5}
530 The wsdl2h tool maps <i>`xsd:time`</i> to a string by default. We can map <i>`xsd:time`</i> to
531 an `unsigned long long` (`ULONG64` or `uint64_t`) integer with microsecond time
534 xsd__time = #import "custom/long_time.h"
536 This type represents 00:00:00.000000 to 23:59:59.999999, from `0` to an upper
537 bound of `86399999999`. A microsecond resolution means that a 1 second
538 increment requires an increment of 1000000 in the integer value. The serializer
539 adds a UTC time zone.
541 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/long_time.c`</i> with your
544 @see Section [date and time types](#toxsd7).
546 🔝 [Back to table of contents](#)
548 ### xsd:duration {#custom-6}
550 The wsdl2h tool maps <i>`xsd:duration`</i> to a string by default, unless <i>`xsd:duration`</i>
551 is mapped to a `long long` (`LONG64` or `int64_t`) type with with millisecond
552 (ms) time duration precision:
554 xsd__duration = #import "custom/duration.h"
556 The `xsd__duration` type is a 64 bit signed integer that can represent
557 106,751,991,167 days forwards (positive) and backwards (negative) in time in
558 increments of 1 ms (1/1000 of a second).
560 Rescaling of the duration value by may be needed when adding the duration value
561 to a `time_t` value, because `time_t` may or may not have a seconds resolution,
562 depending on the platform and possible changes to `time_t`.
564 Rescaling is done automatically when you add a C++11 `std::chrono::nanoseconds`
565 value to a `std::chrono::system_clock::time_point` value. To use
566 `std::chrono::nanoseconds` as <i>`xsd:duration`</i>:
568 xsd__duration = #import "custom/chrono_duration.h"
570 This type can represent 384,307,168 days (2^63 nanoseconds) forwards and
571 backwards in time in increments of 1 ns (1/1,000,000,000 of a second).
573 Certain observations with respect to receiving durations in years and months
574 apply to both of these serializer decoders for <i>`xsd:duration`</i>.
576 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/duration.c`</i> with your
579 @see Section [time duration types](#toxsd8).
581 🔝 [Back to table of contents](#)
583 Custom Qt serializers for XSD types {#qt}
584 -----------------------------------
586 The gSOAP distribution includes several custom serializers for Qt types. Also
587 Qt container classes are supported, see
588 [the built-in typemap.dat variables $CONTAINER, $POINTER and $SIZE](#typemap5).
590 This feature requires gSOAP 2.8.34 or higher and Qt 4.8 or higher.
592 Each Qt custom serializer has an interface header file for soapcpp2 and a C++
593 implementation file to be compiled with your project.
595 Other Qt primitive types that are Qt `typedef`s of C/C++ types do not require a
598 🔝 [Back to table of contents](#)
600 ### xsd:string {#qt-1}
602 To use Qt strings instead of C++ strings, add the following definition to
603 <i>`typemap.dat`</i>:
605 xsd__string = #import "custom/qstring.h"
607 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qstring.cpp`</i> with your
610 🔝 [Back to table of contents](#)
612 ### xsd:base64Binary {#qt-2}
614 To use Qt byte arrays for <i>`xsd:base64Binary`</i> instead of the
615 `xsd__base64Binary` class, add the following definition to <i>`typemap.dat`</i>:
617 xsd__base64Binary = #import "custom/qbytearray_base64.h"
619 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qbytearray_base64.cpp`</i> with
622 🔝 [Back to table of contents](#)
624 ### xsd:hexBinary {#qt-3}
626 To use Qt byte arrays for <i>`xsd:hexBinary`</i> instead of the `xsd__base64Binary`
627 class, add the following definition to <i>`typemap.dat`</i>:
629 xsd__hexBinary = #import "custom/qbytearray_hex.h"
631 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qbytearray_hex.cpp`</i> with
634 🔝 [Back to table of contents](#)
636 ### xsd:dateTime {#qt-4}
638 To use Qt QDateTime for <i>`xsd:dateTime`</i>, add the following definition to
639 <i>`typemap.dat`</i>:
641 xsd__dateTime = #import "custom/datetime.h"
643 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qdatetime.cpp`</i> with
646 🔝 [Back to table of contents](#)
650 To use Qt QDate for <i>`xsd:date`</i>, add the following definition to
651 <i>`typemap.dat`</i>:
653 xsd__date = #import "custom/qdate.h"
655 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qdate.cpp`</i> with your
658 🔝 [Back to table of contents](#)
662 To use Qt QDate for <i>`xsd:time`</i>, add the following definition to
663 <i>`typemap.dat`</i>:
665 xsd__time = #import "custom/qtime.h"
667 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qtime.cpp`</i> with your
670 🔝 [Back to table of contents](#)
672 Class/struct member additions {#typemap3}
673 -----------------------------
675 All generated classes and structs can be augmented with additional
676 members such as methods, constructors and destructors, and private members:
678 prefix__type = $ member-declaration
680 For example, we can add method declarations and private members to a class, say
681 `ns__record` as follows:
683 ns__record = $ ns__record(const ns__record &); // copy constructor
684 ns__record = $ void print(); // a print method
685 ns__record = $ private: int status; // a private member
687 Method declarations cannot include any code, because soapcpp2's input permits
688 only type declarations, not code.
690 🔝 [Back to table of contents](#)
692 Replacing XSD types by equivalent alternatives {#typemap4}
693 ----------------------------------------------
695 Type replacements can be given to replace one type entirely with another given
698 prefix__type1 == prefix__type2
700 This replaces all `prefix__type1` by `prefix__type2` in the wsdl2h output.
702 @warning Do not agressively replace types, because this can cause XML schema
703 validation to fail when a value-type mismatch is encountered in the XML input.
704 Therefore, only replace similar types with other similar types that are wider
705 (e.g. `short` by `int` and `float` by `double`).
707 🔝 [Back to table of contents](#)
709 The built-in typemap.dat variables $CONTAINER, $POINTER and $SIZE {#typemap5}
710 -----------------------------------------------------------------
712 The <i>`typemap.dat`</i> <b>`$CONTAINER`</b> variable defines the container type to use in
713 the wsdl2h-generated declarations for C++, which is `std::vector` by default.
714 For example, to use `std::list` as the container in the wsdl2h-generated
715 declarations we add the following line to <i>`typemap.dat`</i>:
717 $CONTAINER = std::list
719 Also a Qt container can be used instead of the default `std::vector`, for
727 To remove containers, use <b>`wsdl2h -s`</b>. This also removes `std::string`,
728 but you can re-introduce `std::string` with
729 <b>`xsd__string = | std::string`</b> in <i>`typemap.dat`</i>.
731 The <i>`typemap.dat`</i> <b>`$POINTER`</b> variable defines the smart pointer to use in the
732 wsdl2h-generated declarations for C++, which replaces the use of `*` pointers.
735 $POINTER = std::shared_ptr
737 Not all pointers in the generated output are replaced by smart pointers by
738 wsdl2h, such as pointers as union members and pointers as struct/class members
739 that point to arrays of values.
741 @note The standard smart pointer `std::shared_ptr` is generally safe to use.
742 Other smart pointers such as `std::unique_ptr` and `std::auto_ptr` may cause
743 compile-time errors when classes have smart pointer members but no copy
744 constructor (a default copy constructor). A copy constructor is required for
745 non-shared smart pointer copying or swapping.
747 Alternatives to `std::shared_ptr` of the form `NAMESPACE::shared_ptr` can be
748 assigned to <b>`$POINTER`</b> when the namespace `NAMESPACE` also implements
749 `NAMESPACE::make_shared` and when the shared pointer class provides `reset()`
750 and`get()` methods and the dereference operator. For example Boost
754 #include <boost/shared_ptr.hpp>
756 $POINTER = boost::shared_ptr
758 The user-defined content between <b>`[`</b> and <b>`]`</b> ensures that we include the Boost
759 header files that are needed to support `boost::shared_ptr` and
760 `boost::make_shared`.
762 The variable <b>`$SIZE`</b> defines the type of array sizes, which is `int` by
763 default. For example, to change array size types to `size_t`:
767 Permissible types are `int` and `size_t`. This variable does not affect the
768 size of dynamic arrays, `xsd__hexBinary` and `xsd__base64Binary` types, which
771 🔝 [Back to table of contents](#)
773 User-defined content {#typemap6}
776 Any other content to be generated by wsdl2h can be included in <i>`typemap.dat`</i> by
777 enclosing it within brackets <b>`[`</b> and <b>`]`</b> anywhere in the <i>`typemap.dat`</i> file.
778 Each of the two brackets must appear at the start of a new line.
780 For example, we can add an `#import "wsa5.h"` to the wsdl2h-generated output as
784 #import "import/wsa5.h"
787 which emits the `#import "import/wsa5.h"` literally at the start of the
788 wsdl2h-generated header file.
790 🔝 [Back to table of contents](#)
792 Mapping C/C++ to XML schema {#toxsd}
793 ===========================
795 The soapcpp2 command generates the data binding implementation code from a data
796 binding interface <i>`file.h`</i>:
798 soapcpp2 [options] file.h
800 where <i>`file.h`</i> is a interface header file that declares the XML data
801 binding interface. The <i>`file.h`</i> is typically generated by wsdl2h, but
802 you can also declare one yourself. If so, add `//gsoap`
803 [directives](#directives) and declare in this file all our C/C++ types you want
806 You can also declare functions that will be converted to Web service operations
807 by soapcpp2. Global function declarations define service operations, which are
811 int prefix__func(arg1, arg2, ..., argn, result);
814 where `arg1`, `arg2`, ..., `argn` are formal argument declarations of the input
815 and `result` is a formal argument for the output, which must be a pointer or
816 reference to the result object to be populated. More information on declaring
817 and implementing service operation functions can be found in the
818 [gSOAP user guide.](../../guide/html/index.html)
820 🔝 [Back to table of contents](#)
822 Overview of serializable C/C++ types {#toxsd1}
823 ------------------------------------
825 The following C/C++ types are supported by soapcpp2 and mapped to XSD types
826 and constructs. See the subsections below for more details or follow the links.
828 🔝 [Back to table of contents](#)
830 ### List of Boolean types
833 ----------------------------- | -----
835 `enum xsd__boolean` | C alternative to C++ `bool` with `false_` and `true_`
837 @see Section [C++ bool and C alternative](#toxsd3).
839 🔝 [Back to table of contents](#)
841 ### List of enumeration and bitmask types
843 Enumeration Type | Notes
844 ----------------------------- | -----
846 `enum class` | C++11 scoped enumeration, requires `soapcpp2 -c++11`
847 `enum*` | a bitmask that enumerates values 1, 2, 4, 8, ...
848 `enum* class` | C++11 scoped enumeration bitmask, requires `soapcpp2 -c++11`
850 @see Section [enumerations and bitmasks](#toxsd4).
852 🔝 [Back to table of contents](#)
854 ### List of numerical types
856 Numerical Type | Notes
857 ----------------------------- | -----
859 `short` | 16 bit integer
860 `int` | 32 bit integer
861 `long` | 32 bit integer
862 `LONG64` | 64 bit integer
863 `xsd__integer` | 128 bit integer, use `#import "custom/int128.h"`
864 `long long` | same as `LONG64`
865 `unsigned char` | unsigned byte
866 `unsigned short` | unsigned 16 bit integer
867 `unsigned int` | unsigned 32 bit integer
868 `unsigned long` | unsigned 32 bit integer
869 `ULONG64` | unsigned 64 bit integer
870 `unsigned long long` | same as `ULONG64`
871 `int8_t` | same as `char`
872 `int16_t` | same as `short`
873 `int32_t` | same as `int`
874 `int64_t` | same as `LONG64`
875 `uint8_t` | same as `unsigned char`
876 `uint16_t` | same as `unsigned short`
877 `uint32_t` | same as `unsigned int`
878 `uint64_t` | same as `ULONG64`
879 `size_t` | transient type (not serializable)
880 `float` | 32 bit float
881 `double` | 64 bit float
882 `long double` | extended precision float, use `#import "custom/long_double.h"`
883 `xsd__decimal` | `quadmath.h` library 128 bit quadruple precision float, use `#import "custom/float128.h"`
884 `typedef` | declares a type name, with optional value range and string length bounds
886 @see Section [numerical types](#toxsd5).
888 🔝 [Back to table of contents](#)
890 ### List of string types
893 ----------------------------- | -----
894 `char*` | string (may contain UTF-8 with flag `SOAP_C_UTFSTRING`)
895 `wchar_t*` | wide string
896 `std::string` | C++ string (may contain UTF-8 with flag `SOAP_C_UTFSTRING`)
897 `std::wstring` | C++ wide string
898 `char[N]` | fixed-size string, requires `soapcpp2 -b`
899 `_QName` | normalized QName content
900 `_XML` | literal XML string content with wide characters in UTF-8
901 `typedef` | declares a new string type name, may restrict string length
903 @see Section [string types](#toxsd6).
905 🔝 [Back to table of contents](#)
907 ### List of date and time types
909 Date and Time Type | Notes
910 --------------------------------------- | -----
911 `time_t` | date and time point since epoch
912 `struct tm` | date and time point, use `#import "custom/struct_tm.h"`
913 `struct tm` | date point, use `#import "custom/struct_tm_date.h"`
914 `struct timeval` | date and time point, use `#import "custom/struct_timeval.h"`
915 `unsigned long long` | time point in microseconds, use `#import "custom/long_time.h"`
916 `std::chrono::system_clock::time_point` | date and time point, use `#import "custom/chrono_time_point.h"`
918 @see Section [date and time types](#toxsd7).
920 🔝 [Back to table of contents](#)
922 ### List of time duration types
924 Time Duration Type | Notes
925 ----------------------------- | -----
926 `long long` | duration in milliseconds, use `#import "custom/duration.h"`
927 `std::chrono::nanoseconds` | duration in nanoseconds, use `#import "custom/chrono_duration.h"`
929 @see Section [time duration types](#toxsd8).
931 🔝 [Back to table of contents](#)
933 ### List of classes, structs, unions, pointers, containers, and arrays
935 Classes, Structs, and Members | Notes
936 ----------------------------- | -----
937 `class` | C++ class with single inheritance only
938 `struct` | C struct or C++ struct without inheritance
939 `std::shared_ptr<T>` | C++11 smart shared pointer
940 `std::unique_ptr<T>` | C++11 smart pointer
941 `std::auto_ptr<T>` | C++ smart pointer
942 `std::deque<T>` | use `#import "import/stldeque.h"`
943 `std::list<T>` | use `#import "import/stllist.h"`
944 `std::vector<T>` | use `#import "import/stlvector.h"`
945 `std::set<T>` | use `#import "import/stlset.h"`
946 `template<T> class` | a container with `begin()`, `end()`, `size()`, `clear()`, and `insert()` methods
947 `T*` | pointer to data of type `T`
948 `T*` | as a class or struct member: points to data of type `T` or array of `T` with member `__size`
949 `T[N]` | as a class or struct member: fixed-size array of type `T`
950 `union` | as a class or struct member: requires a variant selector member `__union`
951 `void*` | as a class or struct member: requires a `__type` member to indicate the type of object pointed to
953 @see Section [classes and structs](#toxsd9).
955 🔝 [Back to table of contents](#)
957 ### List of special classes and structs
959 Special Classes and Structs | Notes
960 ----------------------------- | -----
961 Special Array class/struct | single and multidimensional SOAP Arrays
962 Special Wrapper class/struct | complexTypes with simpleContent, wraps `__item` member
963 `xsd__hexBinary` | binary content
964 `xsd__base64Binary` | binary content and optional DIME/MIME/MTOM attachments
965 `xsd__anyType` | DOM elements, use `#import "dom.h"`
966 `@xsd__anyAttribute` | DOM attributes, use `#import "dom.h"`
968 @see Section [special classes and structs](#toxsd10).
970 🔝 [Back to table of contents](#)
972 Colon notation versus name prefixing with XML tag name translation {#toxsd2}
973 ------------------------------------------------------------------
975 To bind C/C++ type names to XSD types, a simple form of name prefixing is used
976 by the gSOAP tools by prepending the XML namespace prefix to the C/C++ type
977 name with a pair of undescrores. This also ensures that name clashes cannot
978 occur when multiple WSDL and XSD files are converted to C/C++. Also, C++
979 namespaces are not sufficiently rich to capture XML schema namespaces
980 accurately, for example when class members are associated with schema elements
981 defined in another XML namespace and thus the XML namespace scope of the
982 member's name is relevant, not just its type.
984 However, from a C/C++ centric point of view this can be cumbersome. Therefore,
985 colon notation is an alternative to physically augmenting C/C++ names with
988 For example, the following class uses colon notation to bind the `record` class
989 to the <i>`urn:types`</i> schema:
992 //gsoap ns schema namespace: urn:types
993 class ns:record // binding 'ns:' to a type name
998 ns:record *spouse; // using 'ns:' with the type name
999 ns:record(); // using 'ns:' here too
1000 ~ns:record(); // and here
1004 The colon notation is stripped away by soapcpp2 when generating the data
1005 binding implementation code for our project. So the final code just uses
1006 `record` to identify this class and its constructor/destructor.
1008 When using colon notation make sure to be consistent and not use colon notation
1009 mixed with prefixed forms. The qualified name `ns:record` differs from `ns__record`,
1010 because `ns:record` is compiled to an unqualified `record` name in the source
1011 code output by the soapcpp2 tool.
1013 Colon notation also facilitates overruling the elementFormDefault and
1014 attributeFormDefault declaration that is applied to local elements and
1015 attributes, when declared as members of classes, structs, and unions. For more
1016 details, see [qualified and unqualified members](#toxsd9-6).
1018 A C/C++ identifier name (a type name, member name, function name, or parameter
1019 name) is translated to an XML tag name by the following rules:
1021 - Two leading underscores indicates that the identifier name has no XML tag
1022 name, i.e. this name is not visible in XML and is not translated.
1024 - A leading underscore is removed, but the underscore indicates that: **a**) a
1025 struct/class member name or parameter name has a wildcard XML tag name (i.e.
1026 matches any XML tag), or **b**) a type name that has a
1027 [document root element definition](#toxsd9-7).
1029 - Trailing underscores are removed (i.e. trailing underscores can be used to
1030 avoid name clashes with keywords).
1032 - Underscores within names are translated to hyphens (hyphens are more common
1035 - `_USCORE` is translated to an underscore in the translated XML tag name.
1037 - `_DOT` is translated to a dot (<i>`.`</i>) in the translated XML tag name.
1039 - `_xHHHH` is translated to the Unicode character with code point HHHH (hex).
1041 - C++11 Unicode identifier name characters in UTF-8 are translated as-is.
1043 For example, the C/C++ namespace qualified identifier name `s_a__my_way` is
1044 translated to the XML tag name <i>`s-a:my-way`</i> by translating the prefix `s_a`
1045 and the local name `my_way`.
1047 Struct/class member and parameter name translation can be overruled by using
1048 [backtick XML tags](#toxsd9-5) (with gSOAP 2.8.30 and greater).
1050 🔝 [Back to table of contents](#)
1052 C++ Bool and C alternatives {#toxsd3}
1053 ---------------------------
1055 The C++ `bool` type is bound to built-in XSD type <i>`xsd:boolean`</i>.
1057 The C alternative is to define an enumeration:
1060 enum xsd__boolean { false_, true_ };
1063 or by defining an enumeration in C with pseudo-scoped enumeration constants:
1066 enum xsd__boolean { xsd__boolean__false, xsd__boolean__true };
1069 The XML value space of these types is <i>`false`</i> and <i>`true`</i>, but also accepted
1070 are <i>`0`</i> and <i>`1`</i> values for <i>`false`</i> and <i>`true`</i>, respectively.
1072 To prevent name clashes, `false_` and `true_` have a trailing underscore in
1073 their `enum` symbols. Trailing underscores are removed from the XML value space.
1075 🔝 [Back to table of contents](#)
1077 Enumerations and bitmasks {#toxsd4}
1078 -------------------------
1080 Enumerations are mapped to XSD simpleType enumeration restrictions of
1081 <i>`xsd:string`</i>, <i>`xsd:QName`</i>, and <i>`xsd:long`</i>.
1083 Consider for example:
1086 enum ns__Color { RED, WHITE, BLUE };
1089 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1094 <simpleType name="Color">
1095 <restriction base="xsd:string">
1096 <enumeration value="RED"/>
1097 <enumeration value="WHITE"/>
1098 <enumeration value="BLUE"/>
1104 Enumeration name constants can be pseudo-scoped to prevent name clashes,
1105 because enumeration name constants have a global scope in C and C++:
1108 enum ns__Color { ns__Color__RED, ns__Color__WHITE, ns__Color__BLUE };
1111 You can also use C++11 scoped enumerations to prevent name clashes:
1114 enum class ns__Color : int { RED, WHITE, BLUE };
1117 Here, the enumeration class base type `: int` is optional. In place of `int`
1118 in the example above, we can also use `int8_t`, `int16_t`, `int32_t`, or
1121 The XML value space of the enumertions defined above is <i>`RED`</i>, <i>`WHITE`</i>, and
1124 Prefix-qualified enumeration name constants are mapped to simpleType
1125 restrictions of <i>`xsd:QName`</i>, for example:
1128 enum ns__types { xsd__int, xsd__float };
1131 which maps to a simpleType restriction of <i>`xsd:QName`</i> in the soapcpp2-generated
1136 <simpleType name="types">
1137 <restriction base="xsd:QName">
1138 <enumeration value="xsd:int"/>
1139 <enumeration value="xsd:float"/>
1145 Enumeration name constants can be pseudo-numeric as follows:
1148 enum ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };
1151 which maps to a simpleType restriction of <i>`xsd:long`</i>:
1155 <simpleType name="Color">
1156 <restriction base="xsd:long">
1157 <enumeration value="3"/>
1158 <enumeration value="5"/>
1159 <enumeration value="7"/>
1160 <enumeration value="11"/>
1166 The XML value space of this type is <i>`3`</i>, <i>`5`</i>, <i>`7`</i>, and <i>`11`</i>.
1168 Besides (pseudo-) scoped enumerations, another way to prevent name clashes
1169 accross enumerations is to start an enumeration name constant with one
1170 underscore or followed it by any number of underscores, which makes it
1171 unique. The leading and trailing underscores are removed from the XML value
1175 enum ns__ABC { A, B, C };
1176 enum ns__BA { B, A }; // BAD: B = 1 but B is already defined as 2
1177 enum ns__BA_ { B_, A_ }; // OK
1180 The gSOAP soapcpp2 tool permits reusing enumeration name constants across
1181 (non-scoped) enumerations as long as these values are assigned the same
1182 constant. Therefore, the following is permitted:
1185 enum ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };
1186 enum ns__Throws { _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6 };
1189 A bitmask type is an `enum*` "product enumeration" with a geometric,
1190 power-of-two sequence of values assigned to the enumeration constants:
1193 enum* ns__Options { SSL3, TLS10, TLS11, TLS12, TLS13 };
1196 where the product enum assigns 1 to `SSL3`, 2 to `TLS10`, 4 to `TLS11`, 8
1197 to `TLS12`, and 16 to `TLS13`, which allows these enumeration constants to be
1198 used in composing bitmasks with `|` (bitwise or) `&` (bitwise and), and `~`
1202 enum ns__Options options = (enum ns__Options)(SSL3 | TLS10 | TLS11 | TLS12 | TLS13);
1203 if (options & SSL3) // if SSL3 is an option, warn and remove from options
1210 The bitmask type maps to a simpleType list restriction of <i>`xsd:string`</i> in the
1211 soapcpp2-generated XML schema:
1215 <simpleType name="Options">
1217 <restriction base="xsd:string">
1218 <enumeration value="SSL3"/>
1219 <enumeration value="TLS10"/>
1220 <enumeration value="TLS11"/>
1221 <enumeration value="TLS12"/>
1222 <enumeration value="TLS13"/>
1229 The XML value space of this type consists of all 16 possible subsets of the
1230 four values, represented by an XML string with space-separated values. For
1231 example, the bitmask `TLS10 | TLS11 | TLS12` equals 14 and is represented by
1232 the XML text <i>`TLS10 TLS11 TLS12`</i>.
1234 You can also use C++11 scoped enumerations with bitmasks using `enum*` product
1238 enum* class ns__Options { SSL3, TLS10, TLS11, TLS12, TLS13 };
1241 The base type of a scoped enumeration bitmask, when explicitly given, is
1242 ignored. The base type is either `int` or `int64_t`, depending on the number
1243 of constants enumerated in the bitmask.
1245 To convert `enum` name constants and bitmasks to a string, we use the
1246 auto-generated function for enum `T`:
1249 const char *soap_T2s(struct soap*, enum T val)
1252 The string returned is stored in an internal buffer of the current `soap`
1253 context, so you should copy it to keep it from being overwritten. For example,
1254 use `char *soap_strdup(struct soap*, const char*)`.
1256 To convert a string to an `enum` constant or bitmask, we use the auto-generated
1260 int soap_s2T(struct soap*, const char *str, enum T *val)
1263 This function takes the name (or names, space-separated for bitmasks) of
1264 the enumeration constant in a string `str`. Names should be given without the
1265 pseudo-scope prefix and without trailing underscores. The function sets `val`
1266 to the corresponding integer enum constant or to a bitmask. The function
1267 returns `SOAP_OK` (zero) on success or an error if the string is not a valid
1270 🔝 [Back to table of contents](#)
1272 Numerical types {#toxsd5}
1275 Integer and floating point types are mapped to the equivalent built-in XSD
1276 types with the same sign and bit width.
1278 The `size_t` type is transient (not serializable) because its width is platform
1279 dependent. We recommend to use `uint64_t` instead.
1281 The XML value space of integer types are their decimal representations without
1284 The XML value space of floating point types are their decimal representations.
1285 The decimal representations are formatted with the printf format string `"%.9G"`
1286 for floats and the printf format string `"%.17lG"` for double. To change the
1287 format strings, we can assign new strings to the following `soap` context
1291 soap.float_format = "%g";
1292 soap.double_format = "%lg";
1293 soap.long_double_format = "%Lg";
1296 Decimal representations may result in a loss of precision of the least
1297 significant decimal. Therefore, the format strings that are used by default
1298 are sufficiently precise to avoid loss, but this may result in long decimal
1299 fractions in the XML value space.
1301 The `long double` extended floating point type requires a custom serializer:
1304 #import "custom/long_double.h"
1305 ... // use long double
1308 You can now use `long double`, which has a serializer that serializes this type
1309 as <i>`xsd:decimal`</i>. Compile and link your code with the file
1310 <i>`gsoap/custom/long_double.c`</i>.
1312 The value space of floating point values includes the special values
1313 <i>`INF`</i>, <i>`-INF`</i>, and <i>`NaN`</i>. You can check a value for plus
1314 or minus infinity and not-a-number as follows:
1317 soap_isinf(x) && x > 0 // is x INF?
1318 soap_isinf(x) && x < 0 // is x -INF?
1319 soap_isnan(x) // is x NaN?
1322 To assign these values, use:
1325 // x is float // x is double, long double, or __float128
1326 x = FLT_PINFY; x = DBL_PINFTY;
1327 x = FLT_NINFY; x = DBL_NINFTY;
1328 x = FLT_NAN; x = DBL_NAN;
1331 If your system supports `__float128` then you can also use this 128 bit
1332 floating point type with a custom serializer:
1335 #import "custom/float128.h"
1336 ... // use xsd__decimal
1339 Then use the `xsd__decimal` alias of `__float128`, which has a serializer. Do
1340 not use `__float128` directly, which is transient (not serializable).
1342 To check for <i>`INF`</i>, <i>`-INF`</i>, and <i>`NaN`</i> of a `__float128`
1346 isinfq(x) && x > 0 // is x INF?
1347 isinfq(x) && x < 0 // is x -INF?
1348 isnanq(x) // is x NaN?
1351 The range of a `typedef`-defined numerical type can be restricted using the range
1352 `:` operator with inclusive lower and upper bounds. For example:
1355 typedef int ns__narrow -10 : 10;
1358 This maps to a simpleType restriction of <i>`xsd:int`</i> in the soapcpp2-generated
1363 <simpleType name="narrow">
1364 <restriction base="xsd:int">
1365 <minInclusive value="-10"/>
1366 <maxInclusive value="10"/>
1372 The lower and upper bound of a range are optional. When omitted, values are
1373 not bound from below or from above, respectively.
1375 The range of a floating point `typedef`-defined type can be restricted within
1376 floating point constant bounds.
1378 Also with a floating point `typedef` a `printf`-format pattern can be given of the
1379 form `"%[width][.precision]f"` to format decimal values using the given width
1380 and precision fields:
1383 typedef float ns__PH "%5.2f" 0.0 : 14.0;
1386 This maps to a simpleType restriction of <i>`xsd:float`</i> in the soapcpp2-generated
1391 <simpleType name="PH">
1392 <restriction base="xsd:float">
1393 <totalDigits value="5"/>
1394 <fractionDigits value="2"/>
1395 <minInclusive value="0"/>
1396 <maxInclusive value="14"/>
1402 For exclusive bounds, we use the `<` operator instead of the `:` range
1406 typedef float ns__epsilon 0.0 < 1.0;
1409 Values `eps` of `ns__epsilon` are restricted between `0.0 < eps < 1.0`.
1411 This maps to a simpleType restriction of <i>`xsd:float`</i> in the soapcpp2-generated
1416 <simpleType name="epsilon">
1417 <restriction base="xsd:float">
1418 <minExclusive value="0"/>
1419 <maxExclusive value="1"/>
1425 To make just one of the bounds exclusive, while keeping the other bound
1426 inclusive, we add a `<` on the left or on the right side of the range ':'
1427 operator. For example:
1430 typedef float ns__pos 0.0 < : ; // 0.0 < pos
1431 typedef float ns__neg : < 0.0 ; // neg < 0.0
1434 It is valid to make both left and right side exclusive with `< : <` which is in
1435 fact identical to the exlusive range `<` operator:
1438 typedef float ns__epsilon 0.0 < : < 1.0; // 0.0 < eps < 1.0
1441 It helps to think of the `:` as a placeholder of the value between the two
1442 bounds, which is easier to memorize than the shorthand forms of bounds from
1443 which the `:` is removed:
1445 | bounds | validation check | shorthand |
1446 | ------------ | ---------------- | ----------- |
1447 | `1 : ` | 1 <= x | `1 ` |
1448 | `1 : 10 ` | 1 <= x <= 10 | |
1449 | ` : 10 ` | x <= 10 | |
1450 | `1 < : < 10` | 1 < x < 10 | `1 < 10 ` |
1451 | `1 : < 10` | 1 <= x < 10 | |
1452 | ` : < 10` | x < 10 | ` < 10 ` |
1453 | `1 < : ` | 1 < x | `1 < ` |
1454 | `1 < : 10 ` | 1 < x <= 10 | |
1456 Besides `float`, also `double` and `long double` values can be restricted. For
1457 example, consider a nonzero probability extended floating point precision type:
1460 #import "custom/long_double.h"
1461 typedef long double ns__probability "%16Lg" 0.0 < : 1.0;
1464 Value range restrictions are validated by the parser for all inbound XML data.
1465 A type fault `SOAP_TYPE` will be thrown by the deserializer if the value is out
1468 Finally, if your system supports `__int128_t` then you can also use this 128
1469 bit integer type with a custom serializer:
1472 #import "custom/int128.h"
1473 ... // use xsd__integer
1476 Use the `xsd__integer` alias of `__int128_t`, which has a serializer. Do not
1477 use `__int128_t` directly, which is transient (not serializable).
1479 To convert numeric values to a string, we use the auto-generated function for
1483 const char *soap_T2s(struct soap*, T val)
1486 For numeric types `T`, the string returned is stored in an internal buffer of
1487 the current `soap` context, so you should copy it to keep it from being
1488 overwritten. For example, use `char *soap_strdup(struct soap*, const char*)`.
1490 To convert a string to a numeric value, we use the auto-generated function
1493 int soap_s2T(struct soap*, const char *str, T *val)
1496 where `T` is for example `int`, `LONG64`, `float`, `decimal` (the custom
1497 serializer name of `long double`) or `xsd__integer` (the custom serializer name
1498 of `__int128_t`). The function `soap_s2T` returns `SOAP_OK` on success or an
1499 error when the value is not numeric. For floating point types, `"INF"`, `"-INF"`
1500 and `"NaN"` are valid strings to convert to numbers.
1502 🔝 [Back to table of contents](#)
1504 String types {#toxsd6}
1507 String types are mapped to the built-in <i>`xsd:string`</i> and <i>`xsd:QName`</i> XSD types.
1509 The wide strings `wchar_t*` and `std::wstring` may contain Unicode that is
1510 preserved in the XML value space.
1512 Strings `char*` and `std::string` can only contain extended Latin, but we can
1513 store UTF-8 content that is preserved in the XML value space when the `soap`
1514 context is initialized with the flag `SOAP_C_UTFSTRING`.
1516 @warning Beware that many XML 1.0 parsers reject all control characters (those
1517 between `#x1` and `#x1F`) except for `#x9`, `#xA`, and `#xD`. With the
1518 newer XML 1.1 version parsers (including gSOAP) you should be fine.
1520 The length of a string of a `typedef`-defined string type can be restricted:
1523 typedef std::string ns__password 6 : 16;
1526 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1531 <simpleType name="password">
1532 <restriction base="xsd:string">
1533 <minLength value="6"/>
1534 <maxLength value="16"/>
1540 String length restrictions are validated by the parser for inbound XML data.
1541 A value length fault `SOAP_LENGTH` will be thrown by the deserializer if the
1542 string is too long or too short.
1544 In addition, an XSD regex pattern restriction can be associated with a string
1548 typedef std::string ns__password "([a-zA-Z]|[0-9]|-)+" 6 : 16;
1551 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1556 <simpleType name="password">
1557 <restriction base="xsd:string">
1558 <pattern value="([a-zA-Z0-9]|-)+"/>
1559 <minLength value="6"/>
1560 <maxLength value="16"/>
1566 Pattern restrictions are validated by the parser for inbound XML data only if
1567 the `soap::fsvalidate` and `soap::fwvalidate` callbacks are defined.
1569 Exclusive length bounds can be used with strings:
1572 typedef std::string ns__string255 : < 256; // same as 0 : 255
1575 Fixed-size strings (`char[N]`) are rare occurrences in the wild, but apparently
1576 still used in some projects to store strings. To facilitate fixed-size string
1577 serialization, use <b>`soapcpp2 -b`</b> option <b>`-b`</b>. For example:
1580 typedef char ns__buffer[10]; // requires soapcpp2 option -b
1583 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1588 <simpleType name="buffer">
1589 <restriction base="xsd:string">
1590 <maxLength value="9"/>
1596 Fixed-size strings must contain NUL-terminated text and should not contain raw
1597 binary data. Also, the length limitation is more restrictive for UTF-8 content
1598 (enabled with the `SOAP_C_UTFSTRING`) that requires multibyte character
1599 encodings. As a consequence, UTF-8 content may be truncated to fit.
1601 Raw binary data can be stored in a `xsd__base64Binary` or `xsd__hexBinary`
1602 structure, or transmitted as a MIME attachment.
1604 The built-in `_QName` type is a regular C string type (`char*`) that maps to
1605 <i>`xsd:QName`</i> but has the added advantage that it holds normalized qualified names.
1606 There are actually two forms of normalized QName content, to ensure any QName
1607 is represented accurately:
1614 The first form of string is used when the prefix (and the binding URI) is
1615 defined in the namespace table and is bound to a URI (see the .nsmap file).
1616 The second form is used when the URI is not defined in the namespace table and
1617 therefore no prefix is available to bind and normalize the URI to.
1619 A `_QName` string may contain a sequence of space-separated QName values, not
1620 just one, and all QName values are normalized to the format shown above.
1622 To define a `std::string` base type for <i>`xsd:QName`</i>, we use a `typedef`:
1625 typedef std::string xsd__QName;
1628 The `xsd__QName` string content is normalized, just as with the `_QName`
1631 To serialize strings that contain literal XML content to be reproduced in the
1632 XML value space, use the built-in `_XML` string type, which is a regular C
1633 string type (`char*`) that maps to plain XML CDATA.
1635 To define a `std::string` base type for literal XML content, use a `typedef`:
1638 typedef std::string XML;
1641 Strings can hold any of the values of the XSD built-in primitive types. We can
1642 use a string `typedef` to declare the use of the string type as a XSD built-in
1646 typedef std::string xsd__token;
1649 You must ensure that the string values we populate in this type conform to the
1650 XML standard, which in case of <i>`xsd:token`</i> is the lexical and value spaces of
1651 <i>`xsd:token`</i> are the sets of all strings after whitespace replacement of any
1652 occurrence of `#x9`, `#xA` , and `#xD` by `#x20` and collapsing.
1654 As of version 2.8.49, the gSOAP parser will automatically collapse or replace
1655 the white space content when receiving data for XSD types that require white
1656 space collapsed or replaced. This normalization is applied to strings
1657 directly. The decision to collapse or replace is based on the `typedef` name
1658 corresponding to the built-in string-based XSD type.
1660 To copy `char*` or `wchar_t*` strings with a context that manages the allocated
1661 memory, use functions
1664 char *soap_strdup(struct soap*, const char*)
1665 wchar_t *soap_wstrdup(struct soap*, const wchar_t*)
1668 To convert a wide string to a UTF-8 encoded string, use function
1671 const char* SOAP_FMAC2 soap_wchar2s(struct soap*, const wchar_t *s)
1674 The function allocates and returns a string, with its memory being managed by
1677 To convert a UTF-8 encoded string to a wide string, use function
1680 int soap_s2wchar(struct soap*, const char *from, wchar_t **to, long minlen, long maxlen)
1683 where `to` is set to point to an allocated `wchar_t*` string. Pass `-1` for
1684 `minlen` and `maxlen` to ignore length constraints on the target string. The
1685 function returns `SOAP_OK` or an error when the length constraints are not met.
1687 🔝 [Back to table of contents](#)
1689 Date and time types {#toxsd7}
1692 The C/C++ `time_t` type is mapped to the built-in <i>`xsd:dateTime`</i> XSD type that
1693 represents a date and time within a time zone (typically UTC).
1695 The XML value space contains ISO 8601 Gregorian time instances of the form
1696 <i>`[-]CCYY-MM-DDThh:mm:ss.sss[Z|(+|-)hh:mm]`</i>, where <i>`Z`</i> is the UTC time zone
1697 or a time zone offset <i>`(+|-)hh:mm]`</i> from UTC is used.
1699 A `time_t` value is considered and represented in UTC by the serializer.
1701 Because the `time_t` value range is restricted to dates after 01/01/1970 and
1702 before 2038 assuming `time_t` is a `long` 32 bit, care must be taken to ensure
1703 the range of <i>`xsd:dateTime`</i> values in XML exchanges do not exceed the `time_t`
1706 This restriction does not hold for `struct tm` (<i>`time.h`</i> library), which we can use
1707 to store and exchange a date and time in UTC without date range restrictions.
1708 The serializer uses the `struct tm` members directly for the XML value space of
1709 <i>`xsd:dateTime`</i>:
1714 int tm_sec; // seconds (0 - 60)
1715 int tm_min; // minutes (0 - 59)
1716 int tm_hour; // hours (0 - 23)
1717 int tm_mday; // day of month (1 - 31)
1718 int tm_mon; // month of year (0 - 11)
1719 int tm_year; // year - 1900
1720 int tm_wday; // day of week (Sunday = 0) (NOT USED)
1721 int tm_yday; // day of year (0 - 365) (NOT USED)
1722 int tm_isdst; // is summer time in effect?
1723 char* tm_zone; // abbreviation of timezone (NOT USED)
1727 You will lose the day of the week information. It is always Sunday
1728 (`tm_wday=0`) and the day of the year is not set either. The time zone is UTC.
1730 This `struct tm` type is mapped to the built-in <i>`xsd:dateTime`</i> XSD type and
1731 serialized with the custom serializer <i>`gsoap/custom/struct_tm.h`</i> that declares a
1732 `xsd__dateTime` type:
1735 #import "custom/struct_tm.h" // import typedef struct tm xsd__dateTime;
1736 ... // use xsd__dateTime
1739 Compile and link your code with <i>`gsoap/custom/struct_tm.c`</i>.
1741 The `struct timeval` (<i>`sys/time.h`</i> library) type is mapped to the
1742 built-in <i>`xsd:dateTime`</i> XSD type and serialized with the custom serializer
1743 <i>`gsoap/custom/struct_timeval.h`</i> that declares a `xsd__dateTime` type:
1746 #import "custom/struct_timeval.h" // import typedef struct timeval xsd__dateTime;
1747 ... // use xsd__dateTime
1750 Compile and link your code with <i>`gsoap/custom/struct_timeval.c`</i>.
1752 The same value range restrictions apply to `struct timeval` as they apply to
1753 `time_t`. The added benefit of `struct timeval` is the addition of a
1754 microsecond-precise clock:
1759 time_t tv_sec; // seconds since Jan. 1, 1970
1760 suseconds_t tv_usec; // and microseconds
1764 A C++11 `std::chrono::system_clock::time_point` type is mapped to the built-in
1765 <i>`xsd:dateTime`</i> XSD type and serialized with the custom serializer
1766 <i>`gsoap/custom/chrono_time_point.h`</i> that declares a `xsd__dateTime` type:
1769 #import "custom/chrono_time_point.h" // import typedef std::chrono::system_clock::time_point xsd__dateTime;
1770 ... // use xsd__dateTime
1773 Compile and link your code with <i>`gsoap/custom/chrono_time_point.cpp`</i>.
1775 The `struct tm` type is mapped to the built-in <i>`xsd:date`</i> XSD type and serialized
1776 with the custom serializer <i>`gsoap/custom/struct_tm_date.h`</i> that declares a
1780 #import "custom/struct_tm_date.h" // import typedef struct tm xsd__date;
1781 ... // use xsd__date
1784 Compile and link your code with <i>`gsoap/custom/struct_tm_date.c`</i>.
1786 The XML value space of <i>`xsd:date`</i> are Gregorian calendar dates of the form
1787 <i>`[-]CCYY-MM-DD[Z|(+|-)hh:mm]`</i> with a time zone.
1789 The serializer ignores the time part and the deserializer only populates the
1790 date part of the struct, setting the time to 00:00:00. There is no unreasonable
1791 limit on the date range because the year field is stored as an integer (`int`).
1793 An `unsigned long long` (`ULONG64` or `uint64_t`) type that contains a 24 hour
1794 time in microseconds UTC is mapped to the built-in <i>`xsd:time`</i> XSD type and
1795 serialized with the custom serializer <i>`gsoap/custom/long_time.h`</i> that declares a
1799 #import "custom/long_time.h" // import typedef unsigned long long xsd__time;
1800 ... // use xsd__time
1803 Compile and link your code with <i>`gsoap/custom/long_time.c`</i>.
1805 This type represents `00:00:00.000000` to `23:59:59.999999`, from 0 to an
1806 upper bound of 86,399,999,999. A microsecond resolution means that a 1 second
1807 increment requires an increment of 1,000,000 in the integer value.
1809 The XML value space of <i>`xsd:time`</i> are points in time recurring each day of the
1810 form <i>`hh:mm:ss.sss[Z|(+|-)hh:mm]`</i>, where <i>`Z`</i> is the UTC time zone or a time
1811 zone offset from UTC is used. The `xsd__time` value is always considered and
1812 represented in UTC by the serializer.
1814 To convert date and/or time values to a string, we use the auto-generated
1815 function for type `T`:
1818 const char *soap_T2s(struct soap*, T val)
1821 For date and time types `T`, the string returned is stored in an internal
1822 buffer of the current `soap` context, so you should copy it to keep it from being
1823 overwritten. For example, use `char *soap_strdup(struct soap*, const char*)`.
1825 To convert a string to a date/time value, we use the auto-generated function
1828 int soap_s2T(struct soap*, const char *str, T *val)
1831 where `T` is for example `dateTime` (for `time_t`), `xsd__dateTime` (for
1832 `struct tm`, `struct timeval`, or `std::chrono::system_clock::time_point`).
1833 The function `soap_s2T` returns `SOAP_OK` on success or an error when the value
1836 🔝 [Back to table of contents](#)
1838 Time duration types {#toxsd8}
1841 The XML value space of <i>`xsd:duration`</i> are values of the form <i>`PnYnMnDTnHnMnS`</i>
1842 where the capital letters are delimiters. Delimiters may be omitted when the
1843 corresponding member is not used.
1845 A `long long` (`LONG64` or `int64_t`) type that contains a duration (time
1846 lapse) in milliseconds is mapped to the built-in <i>`xsd:duration`</i> XSD type and
1847 serialized with the custom serializer <i>`gsoap/custom/duration.h`</i> that declares a
1848 `xsd__duration` type:
1851 #import "custom/duration.h" // import typedef long long xsd__duration;
1852 ... // use xsd__duration
1855 Compile and link your code with <i>`gsoap/custom/duration.c`</i>.
1857 The duration type `xsd__duration` can represent 106,751,991,167 days forward
1858 and backward with millisecond precision.
1860 Durations that exceed a month are always output in days, rather than months to
1861 avoid days-per-month conversion inacurracies.
1863 Durations that are received in years and months instead of total number of days
1864 from a reference point are not well defined, since there is no accepted
1865 reference time point (it may or may not be the current time). The decoder
1866 simple assumes that there are 30 days per month. For example, conversion of
1867 "P4M" gives 120 days. Therefore, the durations "P4M" and "P120D" are assumed
1868 to be identical, which is not necessarily true depending on the reference point
1871 Rescaling of the duration value by may be needed when adding the duration value
1872 to a `time_t` value, because `time_t` may or may not have a seconds resolution,
1873 depending on the platform and possible changes to `time_t`.
1875 Rescaling is done automatically when you add a C++11 `std::chrono::nanoseconds`
1876 value to a `std::chrono::system_clock::time_point` value. To use
1877 `std::chrono::nanoseconds` as <i>`xsd:duration`</i>:
1880 #import "custom/chrono_duration.h" // import typedef std::chrono::duration xsd__duration;
1881 ... // use xsd__duration
1884 Compile and link your code with <i>`gsoap/custom/chrono_duration.cpp`</i>.
1886 This type can represent 384,307,168 days (2^63 nanoseconds) forwards and
1887 backwards in time in increments of 1 ns (1/1000000000 second).
1889 The same observations with respect to receiving durations in years and months
1890 apply to this serializer's decoder.
1892 To convert duration values to a string, we use the auto-generated function
1895 const char *soap_xsd__duration2s(struct soap*, xsd__duration val)
1898 The string returned is stored in an internal buffer, so you should copy it to
1899 keep it from being overwritten, Use `soap_strdup(struct soap*, const char*)`
1900 for example to copy this string.
1902 To convert a string to a duration value, we use the auto-generated function
1905 int soap_s2xsd__dateTime(struct soap*, const char *str, xsd__dateTime *val)
1908 The function returns `SOAP_OK` on success or an error when the value is not a
1911 🔝 [Back to table of contents](#)
1913 Classes and structs {#toxsd9}
1916 Classes and structs are mapped to XSD complexTypes. The XML value space
1917 consists of XML elements with attributes and subelements, possibly constrained
1918 by XML schema validation rules that enforce element and attribute occurrence
1919 contraints, numerical value range constraints, and string length and pattern
1922 Classes that are declared with the gSOAP tools are limited to single
1923 inheritence only. The soapcpp2 tool does not allow structs to be inherited.
1925 The class and struct name is bound to an XML namespace by means of the prefix
1926 naming convention or by using [colon notation](#toxsd1):
1929 //gsoap ns schema namespace: urn:types
1943 In the example above, we also added a context pointer to the `soap` context that
1944 manages this instance. It is set when the instance is created in the engine's
1945 context, for example when deserialized and populated by the engine.
1947 The class maps to a complexType in the soapcpp2-generated XML schema:
1951 <complexType name="record">
1953 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
1954 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
1955 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
1961 The following sections apply to both structs and classes. Structs require the
1962 use of the `struct` keyword with the struct name, otherwise soapcpp2 will throw
1963 a syntax error. As is often done in C, use a `typedef` to declare a `struct`
1964 that can be used without the `struct` keyword.
1966 🔝 [Back to table of contents](#)
1968 ### Serializable versus transient types and data members {#toxsd9-1}
1970 Public data members of a class or struct are serialized. Private and protected
1971 members are transient and not serializable.
1973 Also `const` and `static` members are not serializable, with the exception of
1974 `const char*` and `const wchar_t*`. Types and specific class/struct members
1975 can also be made transient with the `extern` qualifier:
1978 extern class std::ostream; // declare 'std::ostream' transient
1982 extern int num; // not serialized
1983 std::ostream out; // not serialized
1984 static const int MAX = 1024; // not serialized
1988 By declaring `std::ostream` transient with `extern` you can use this type
1989 wherever you need it without soapcpp2 complaining that this class is not
1992 🔝 [Back to table of contents](#)
1994 ### Volatile classes and structs {#toxsd9-2}
1996 Classes and structs can be declared `volatile` with the gSOAP tools. This means
1997 that they are already declared elsewhere in your project's source code and you
1998 do not want soapcpp2 to generate code with a second declaration of these types.
2000 For example, `struct tm` is declared in the <i>`time.h`</i> library. You can
2001 make it serializable and include a partial list of data members that you want
2007 int tm_sec; // seconds (0 - 60)
2008 int tm_min; // minutes (0 - 59)
2009 int tm_hour; // hours (0 - 23)
2010 int tm_mday; // day of month (1 - 31)
2011 int tm_mon; // month of year (0 - 11)
2012 int tm_year; // year - 1900
2016 You can declare classes and structs `volatile` for any such types you want to
2017 serialize by only providing the public data members you want to serialize.
2019 In addition, [colon notation](#toxsd2) is a simple and effective way to bind an
2020 existing class or struct to a schema. For example, you can change the `tm` name
2021 as follows without affecting the code that uses `struct tm` generated by
2025 volatile struct ns:tm { ... }
2028 This struct maps to a complexType in the soapcpp2-generated XML schema:
2032 <complexType name="tm">
2034 <element name="tm-sec" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2035 <element name="tm-min" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2036 <element name="tm-hour" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2037 <element name="tm-mday" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2038 <element name="tm-mon" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2039 <element name="tm-year" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2045 🔝 [Back to table of contents](#)
2047 ### Mutable classes and structs {#toxsd9-3}
2049 Classes and structs can be declared `mutable` with the gSOAP tools. This means
2050 that their definition can be spread out over the source code. This promotes the
2051 concept of a class or struct as a *row of named values*, also known as a *named
2052 tuple*, that can be extended at compile time in your source code with additional
2053 members. Because these types differ from the traditional object-oriented
2054 principles and design concepts of classes and objects, constructors and
2055 destructors cannot be defined (also because we cannot guarantee merging these
2056 into one such that all members will be initialized). A default constructor,
2057 copy constructor, assignment operation, and destructor will be assigned
2058 automatically by soapcpp2.
2061 mutable struct ns__tuple
2066 mutable struct ns__tuple
2073 The members are collected into one definition generated by soapcpp2. Members
2074 may be repeated from one definition to another, but only if their associated
2075 types are identical. So, for example, a third extension with a `value` member
2076 with a different type fails:
2079 mutable struct ns__tuple
2081 float value; // BAD: value is already declared std::string
2085 The `mutable` concept has proven to be very useful when declaring and
2086 collecting SOAP Headers for multiple services, which are collected into one
2087 `struct SOAP_ENV__Header` by the soapcpp2 tool.
2089 🔝 [Back to table of contents](#)
2091 ### Default and fixed member values {#toxsd9-4}
2093 Class and struct data members in C and C++ may be declared with an optional
2094 default initialization value that is provided "inline" with the declaration of
2101 std::string name = "Joe";
2106 Alternatively, use C++11 default initialization syntax:
2112 std::string name { "Joe" };
2117 These initializations are made by the default constructor that is added by
2118 soapcpp2 to each class and struct (in C++ only). A constructor is only added
2119 when a default constructor is not already defined with the class declaration.
2121 You can explicitly (re)initialize an object with these initial values by using
2122 the soapcpp2 auto-generated functions:
2124 - `void T::soap_default(struct soap*)` for `class T` (C++ only)
2126 - `void soap_default_T(struct soap*, T*)` for `struct T` (C and C++).
2128 If `T` is a struct or class that has a `soap` pointer member to a `::soap`
2129 context then this pointer member will be set to the first argument passed to
2130 these functions to initialize their `soap` pointer member.
2132 Default value initializations can be provided for members that have primitive
2133 types (`bool`, `enum`, `time_t`, numeric and string types).
2135 Default value initializations of pointer members is permitted, but the effect
2136 is different. To conform to XML schema validation, an attribute member that is
2137 a pointer to a primitive type will be assigned the default value when parsed
2138 from XML. An element member that is a pointer to a primitive type will be
2139 assigned when the element is empty when parsed from XML.
2141 As of gSOAP 2.8.48 and greater, a fixed value can be assigned with a `==`. A
2142 fixed value is also verified by the parser's validator.
2144 Default and fixed values for members with or without pointers are best
2145 explained with the following two example fragments.
2147 A record class (can be a struct in C) with default values for attributes and
2148 elements is declared as follows:
2151 class ns__record_with_default
2154 @std::string a = "A"; // optional XML attribute with default value "A"
2155 @std::string b 1 = "B"; // required XML attribute with default value "B"
2156 @std::string *c = "C"; // optional XML attribute with default value "C"
2157 std::string d 0 = "D"; // optional XML element with default value "D"
2158 std::string e = "E"; // required XML element with default value "E"
2159 std::string *f = "F"; // optional XML element with default value "F"
2164 Attributes are optional by default unless marked as required with the
2165 occurrence constraint `1`. Elements are required unless the member type is a
2166 pointer or if the member is marked optional with occurrence constraint `0`.
2168 Instead of default values, fixed values indicate that the attribute or element
2169 must contain that value, and only that value, when provided in XML. A fixed
2170 value is specified with a `==`.
2172 A record class (can be a struct in C) with fixed values for attributes and
2173 elements is declared as follows:
2176 class ns__record_with_fixed
2179 @std::string g == "G"; // optional XML attribute with fixed value "G"
2180 @std::string h 1 == "H"; // required XML attribute with fixed value "H"
2181 @std::string *i == "I"; // optional XML attribute with fixed value "I"
2182 std::string j 0 == "J"; // optional XML element with fixed value "J"
2183 std::string k == "K"; // required XML element with fixed value "K"
2184 std::string *l == "L"; // optional XML element with fixed value "L"
2189 The XML schema validation rules for the two example classes above are as
2193 ------ | ---------------------------------------------------------------------
2194 `a` | attribute may appear once; if it does not appear its value is "A", otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns the default value "A")
2195 `b` | has no effect when parsing XML (but note: instantiating `ns__record_with_default` assigns the default value "B")
2196 `c` | attribute may appear once; if it does not appear its value is "C", otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns NULL)
2197 `d` | element may appear once; if it does not appear or if it is empty, its value is "D"; otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns the default value "D")
2198 `e` | has no effect when parsing XML (but note: instantiating `ns__record_with_default` assigns the default value "E")
2199 `f` | element may appear once; if it does not appear it is not provided; if it does appear and it is empty, its value is "F"; otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns NULL)
2200 `g` | attribute may appear once; if it does not appear its value is "G", if it does not appear its value is "G" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "G")
2201 `h` | attribute must appear once, its value must be "H" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "H")
2202 `i` | attribute may appear once; if it does not appear its value is "I", if it does not appear its value is "I" (also note: instantiating `ns__record_with_fixed` assigns NULL)
2203 `j` | element may appear once, if it does not appear it is not provided; if it does appear and it is empty, its value is "J"; if it does appear and it is not empty, its value must be "J" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "J")
2204 `k` | element must appear once, its value must be "K" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "K")
2205 `l` | element may appear once, if it does not appear it is not provided; if it does appear and it is empty, its value is "J"; if it does appear and it is not empty, its value must be "J" (also note: instantiating `ns__record_with_fixed` assigns NULL)
2207 @see Section [operations on classes and structs](#toxsd9-14).
2209 🔝 [Back to table of contents](#)
2211 ### Attribute members and backtick XML tags {#toxsd9-5}
2213 Class and struct data members are declared as XML attributes by annotating
2214 their type with a `@` qualifier:
2220 @std::string name; // required (non-pointer means required)
2221 @uint64_t SSN; // required (non-pointer means required)
2222 ns__record *spouse; // optional (pointer means minOccurs=0)
2226 This class maps to a complexType in the soapcpp2-generated XML schema:
2230 <complexType name="record">
2232 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
2234 <attribute name="name" type="xsd:string" use="required"/>
2235 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
2240 An example XML instance of `ns__record` is:
2244 <ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890">
2245 <spouse name="Jane" SSN="1987654320">
2251 Attribute data members are restricted to primitive types (`bool`, `enum`,
2252 `time_t`, numeric and string types), `xsd__hexBinary`, `xsd__base64Binary`, and
2253 custom serializers, such as `xsd__dateTime`. Custom serializers for types that
2254 may be used as attributes should define `soap_s2T` and `soap_T2s` functions that
2255 convert values of type `T` to strings and back.
2257 Attribute data members can be pointers and smart pointers to these types, which
2258 permits attributes to be optional.
2260 The XML tag name of a class/struct member is the name of the member with the
2261 usual XML tag translation, see [colon notation](#toxsd2).
2263 To override the standard translation of identifier names to XML tag names of
2264 attributes and elements, add the XML tag name in backticks (requires gSOAP
2271 @std::string name `full-name`;
2272 @uint64_t SSN `tax-id`;
2273 ns__record *spouse `married-to`;
2277 This class maps to a complexType in the soapcpp2-generated XML schema:
2281 <complexType name="record">
2283 <element name="married-to" type="ns:record" minOccurs="0" maxOccurs="1"/>
2285 <attribute name="full-name" type="xsd:string" use="required"/>
2286 <attribute name="tax-id" type="xsd:unsignedLong" use="required"/>
2291 An example XML instance of `ns__record` is:
2295 <ns:record xmlns:ns="urn:types" full-name="Joe" tax-id="1234567890">
2296 <married-to full-name="Jane" tax-id="1987654320">
2302 A backtick XML tag name may contain any non-empty sequence of ASCII and UTF-8
2303 characters except white space and the backtick character. A backtick tag can
2304 be combined with member constraints and default member initializers:
2307 @uint64_t SSN `tax-id` 0:1 = 999;
2310 🔝 [Back to table of contents](#)
2312 ### Qualified and unqualified members {#toxsd9-6}
2314 Class, struct, and union data members are mapped to namespace qualified or
2315 unqualified tag names of local elements and attributes. If a data member has
2316 no prefix then the default form of qualification is applied based on the
2317 element/attribute form that is declared with the XML schema of the class, struct,
2318 or union type. If the member name has a namespace prefix by colon notation,
2319 then the prefix overrules the default (un)qualified form. Therefore,
2320 [colon notation](#toxsd2) is an effective mechanism to control qualification of
2321 tag names of individual members of classes, structs, and unions.
2323 The XML schema elementFormDefault and attributeFormDefault declarations control
2324 the tag name qualification of local elements and attributes, respectively.
2326 - "unqualified" indicates that local elements/attributes are not qualified with
2327 the namespace prefix.
2329 - "qualified" indicates that local elements/attributes must be qualified with
2330 the namespace prefix.
2332 Individual schema declarations of local elements and attributes may overrule
2333 this by using the form declaration in an XML schema and by using colon notation
2334 to add namespace prefixes to class, struct, and union members in the header
2337 Consider for example an `ns__record` class in the `ns` namespace in which local
2338 elements are qualified and local attributes are unqualified by default:
2341 //gsoap ns schema namespace: urn:types
2342 //gsoap ns schema elementForm: qualified
2343 //gsoap ns schema attributeForm: unqualified
2353 This class maps to a complexType in the soapcpp2-generated XML schema with
2354 targetNamespace "urn:types", elementFormDefault qualified and
2355 attributeFormDefault unqualified:
2359 <schema targetNamespace="urn:types"
2361 elementFormDefault="qualified"
2362 attributeFormDefault="unqualified"
2364 <complexType name="record">
2366 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1"/>
2368 <attribute name="name" type="xsd:string" use="required"/>
2369 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
2375 An example XML instance of `ns__record` is:
2379 <ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890">
2380 <ns:spouse> name="Jane" SSN="1987654320">
2386 Here the root element <i>`<ns:record>`</i> is qualified because it is a root
2387 element of the XML schema with target namespace "urn:types". Its local element
2388 <i>`<ns:spouse>`</i> is namespace qualified because the elementFormDefault of
2389 local elements is qualified. Attributes are unqualified.
2391 The default namespace (un)qualification of local elements and attributes can be
2392 overruled by adding a prefix to the member name by using colon notation:
2395 //gsoap ns schema namespace: urn:types
2396 //gsoap ns schema elementForm: qualified
2397 //gsoap ns schema attributeForm: unqualified
2401 @std::string ns:name; // 'ns:' qualified
2403 ns__record *:spouse; // ':' unqualified (empty prefix)
2407 The colon notation for member <i>`ns:name`</i> forces qualification of its attribute
2408 tag in XML. The colon notation for member <i>`:spouse`</i> removes qualification from
2409 its local element tag:
2413 <schema targetNamespace="urn:types"
2415 elementFormDefault="unqualified"
2416 attributeFormDefault="unqualified"
2418 <complexType name="record">
2420 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" form="unqualified"/>
2422 <attribute name="name" type="xsd:string" use="required" form="qualified"/>
2423 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
2429 XML instances of `ns__record` have unqualified spouse elements and qualified
2434 <ns:record xmlns:ns="urn:types" ns:name="Joe" SSN="1234567890">
2435 <spouse> ns:name="Jane" SSN="1987654320">
2441 Members of a class or struct can also be prefixed using the `prefix__name`
2442 convention or using colon notation `prefix:name`. However, this has a
2443 different effect by referring to global (root) elements and attributes, see
2444 [document root element definitions](#toxsd9-7).
2446 [Backtick tag names](#toxsd9-5) can be used in place of the member name
2447 annotations and will achieve the same effect as described when these tag names
2448 are (un)qualified (requires gSOAP 2.8.30 or greater).
2450 @note You must declare a target namespace with a `//gsoap ns schema namespace:`
2451 directive to enable the `elementForm` and `attributeForm` directives in order
2452 to generate valid XML schemas with soapcpp2. See [directives](#directives) for
2455 🔝 [Back to table of contents](#)
2457 ### Defining document root elements {#toxsd9-7}
2459 To define and reference XML document root elements we use type names that start
2466 Alternatively, we can use a `typedef` to define a document root element with a
2470 typedef ns__record _ns__record;
2473 This `typedef` maps to a global root element that is added to the
2474 soapcpp2-generated XML schema:
2478 <element name="record" type="ns:record"/>
2482 An example XML instance of `_ns__record` is:
2486 <ns:record xmlns:ns="urn:types">
2488 <SSN>1234567890</SSN>
2491 <SSN>1987654320</SSN>
2497 Global-level element/attribute definitions are also referenced and/or added to
2498 the generated XML schema when serializable data members reference these by
2499 their qualified name:
2502 typedef std::string _ns__name 1 : 100;
2506 @_QName xsi__type; // built-in XSD attribute xsi:type
2507 _ns__name ns__name; // ref to global ns:name element
2509 _ns__record *spouse;
2513 These types map to the following comonents in the soapcpp2-generated XML
2518 <simpleType name="name">
2519 <restriction base="xsd:string">
2520 <minLength value="1"/>
2521 <maxLength value="100"/>
2524 <element name="name" type="ns:name"/>
2525 <complexType name="record">
2527 <element ref="ns:name" minOccurs="1" maxOccurs="1"/>
2528 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2529 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1"/>
2531 <attribute ref="xsi:type" use="optional"/>
2533 <element name="record" type="ns:record"/>
2537 Use only use qualified member names when their types match the global-level
2538 element types that they refer to. For example:
2541 typedef std::string _ns__name; // global element ns:name of type xsd:string
2545 int ns__name; // BAD: global element ns:name is NOT type int
2546 _ns__record ns__record; // OK: ns:record is a global-level root element
2551 Therefore, we recommend to use qualified member names only when necessary to
2552 refer to standard XSD elements and attributes, such as `xsi__type`, and
2555 By contrast, colon notation has the desired effect to (un)qualify local tag
2556 names by overruling the default element/attribute namespace qualification, see
2557 [qualified and unqualified members](#toxsd9-6).
2559 As an alternative to prefixing member names, use the backtick tag (requires
2560 gSOAP 2.8.30 or greater):
2563 typedef std::string _ns__name 1 : 100;
2567 @_QName t <i>`xsi:type`</i>; // built-in XSD attribute xsi:type
2568 _ns__name s <i>`ns:name`</i>; // ref to global ns:name element
2570 _ns__record *spouse;
2574 🔝 [Back to table of contents](#)
2576 ### (Smart) pointer members and their occurrence constraints {#toxsd9-8}
2578 A public pointer-typed data member is serialized by following its (smart)
2579 pointer(s) to the value pointed to. To serialize pointers to dynamic arrays of
2580 data, please see the next section on
2581 [container and array members and their occurrence constraints](#toxsd9-9).
2583 Pointers that are NULL and smart pointers that are empty are serialized to
2584 produce omitted element and attribute values, unless an element is required
2585 and is nillable (struct/class members marked with `nullptr`) in which case the
2586 element is rendered as an empty element with <i>`xsi:nil="true"`</i>.
2588 To control the occurrence requirements of pointer-based data members,
2589 occurrence constraints are associated with data members in the form of a range
2590 `minOccurs : maxOccurs`. For non-repeatable (meaning, not a container or array)
2591 data members, there are only three reasonable occurrence constraints:
2593 - `0:0` means that this element or attribute is prohibited.
2595 - `0:1` means that this element or attribute is optional.
2597 - `1:1` means that this element or attribute is required.
2599 Pointer-based data members have a default `0:1` occurrence constraint, making
2600 them optional, and their XML schema local element/attribute definition is
2601 marked as nillable. Non-pointer data members have a default `1:1` occurence
2602 constraint, making them required.
2604 A `nullptr` occurrence constraint may be applicable to required elements that
2605 are nillable pointer types, thus `nullptr 1:1`. This indicates that the
2606 element is nillable (can be `NULL` or `nullptr`). A pointer data member that
2607 is explicitly marked as required and nillable with `nullptr 1:1` will be
2608 serialized as an element with an <i>`xsi:nil`</i> attribute, thus effectively
2609 revealing the NULL property of its value.
2611 A non-pointer data member that is explicitly marked as optional with `0:1` will
2612 be set to its default value when no XML value is presented to the deserializer.
2613 A default value can be assigned to a data member that has a primitive type or
2614 is a (smart) pointer to primitive type.
2616 Consider for example:
2622 std::shared_ptr<std::string> name; // optional (pointer means minOccurs=0)
2623 uint64_t SSN 0:1 = 999; // force optional with default 999
2624 ns__record *spouse nullptr 1:1; // force required and nillabe when absent
2628 This class maps to a complexType in the soapcpp2-generated XML schema:
2632 <complexType name="record">
2634 <element name="name" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
2635 <element name="SSN" type="xsd:unsignedLong" minOccurs="0" maxOccurs="1" default="999"/>
2636 <element name="spouse" type="ns:record" minOccurs="1" maxOccurs="1" nillable="true"/>
2642 An example XML instance of `ns__record` with its `name` string value set to
2643 `Joe`, `SSN` set to its default, and `spouse` set to NULL:
2647 <ns:record xmlns:ns="urn:types" ...>
2650 <spouse xsi:nil="true"/>
2655 @note In general, a smart pointer is simply declared as a `volatile` template
2656 in a interface header file for soapcpp2:
2658 volatile template <class T> class NAMESPACE::shared_ptr;
2661 @note The soapcpp2 tool generates code that uses `NAMESPACE::shared_ptr` and
2662 `NAMESPACE::make_shared` to create shared pointers to objects, where
2663 `NAMESPACE` is any valid C++ namespace such as `std` and `boost` if you have
2666 🔝 [Back to table of contents](#)
2668 ### Container and array members and their occurrence constraints {#toxsd9-9}
2670 Class and struct data member types that are containers `std::deque`,
2671 `std::list`, `std::vector` and `std::set` are serialized as a collection of
2672 the values they contain. You can also serialize dynamic arrays, which is the
2673 alternative for C to store collections of data. Let's start with STL containers.
2675 You can use `std::deque`, `std::list`, `std::vector`, and `std::set` containers
2679 #import "import/stl.h" // import all containers
2680 #import "import/stldeque.h" // import deque
2681 #import "import/stllist.h" // import list
2682 #import "import/stlvector.h" // import vector
2683 #import "import/stlset.h" // import set
2686 For example, to use a vector data mamber to store names in a record:
2689 #import "import/stlvector.h"
2693 std::vector<std::string> names;
2698 To limit the number of names in the vector within reasonable bounds, occurrence
2699 constraints are associated with the container. Occurrence constraints are of
2700 the form `minOccurs : maxOccurs`:
2703 #import "import/stlvector.h"
2707 std::vector<std::string> names 1:10;
2712 This class maps to a complexType in the soapcpp2-generated XML schema:
2716 <complexType name="record">
2718 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="10"/>
2719 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2725 @note In general, a container is simply declared as a template in an interface
2726 header file for soapcpp2. All class templates are considered containers
2727 (except when declared `volatile`, see smart pointers). For example,
2728 `std::vector` is declared in <i>`gsoap/import/stlvector.h`</i> as:
2730 template <class T> class std::vector;
2733 @note You can define and use your own containers. The soapcpp2 tool generates
2734 code that uses the following members of the `template <typename T> class C`
2738 C::iterator C::begin()
2739 C::const_iterator C::begin() const
2740 C::iterator C::end()
2741 C::const_iterator C::end() const
2742 size_t C::size() const
2743 C::iterator C::insert(C::iterator pos, const T& val)
2746 @note For more details see the example `simple_vector` container with
2747 documentation in the package under <i>`gsoap/samples/template`</i>.
2749 Because C does not support a container template library, we can use a
2750 dynamically-sized array of values. This array is declared as a size-pointer
2751 pair of members within a struct or class. The array size information is stored
2752 in a special size tag member with the name `__size` or `__sizeX`, where `X` can
2753 be any name, or by an `$int` member to identify the member as a special size
2759 $int sizeofnames; // array size
2760 char* *names; // array of char* names
2765 This struct maps to a complexType in the soapcpp2-generated XML schema:
2769 <complexType name="record">
2771 <element name="name" type="xsd:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
2772 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2778 To limit the number of names in the array within reasonable bounds, occurrence
2779 constraints are associated with the array size member. Occurrence constraints
2780 are of the form `minOccurs : maxOccurs`:
2785 $int sizeofnames 1:10; // array size 1..10
2786 char* *names; // array of one to ten char* names
2791 This struct maps to a complexType in the soapcpp2-generated XML schema:
2795 <complexType name="record">
2797 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="10" nillable="true"/>
2798 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2804 Arrays can also be declared as nested elements, similar to SOAP-encoded dynamic arrays, and these arrays can be used with or without SOAP applications. This requires a separate struct or class with the name of the SOAP array, which should not be qualified with a namespace prefix:
2807 struct ArrayOfstring
2809 char* *__ptr 1:100; // array of 1..100 strings
2810 int __size; // array size
2814 struct ArrayOfstring names; // array of char* names
2819 The `ns__record` struct maps to a complexType that references the <i>`ArrayOfstring`</i> complexType with an sequence of 1 to 100 <i>`item`</i> elements:
2823 <complexType name="ArrayOfstring">
2825 <element name="item" type="xsd:string" minOccurs="1" maxOccurs="100"/>
2828 <complexType name="record">
2830 <element name="names" type="ns:ArrayOfstring" minOccurs="1" maxOccurs="1"/>
2831 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2837 To change the <i>`item`</i> element name in the WSDL, XML schema, and XML messages, use `__ptrName` where `Name` is the name you want to use.
2839 @note When <b>`soapcpp2 -e`</b> option <b>`-e`</b> is used, the <i>`ArrayOfstring`</i> becomes a SOAP-encoded array for SOAP 1.1/1.2 RPC encoded messaging:
2842 <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
2843 <complexType name="ArrayOfstring">
2845 <restriction base="SOAP-ENC:Array">
2847 <element name="item" type="xsd:string" minOccurs="1" maxOccurs="100"/>
2849 <attribute ref="SOAP-ENC:arrayType" WSDL:arrayType="xsd:string[]"/>
2853 <complexType name="record">
2855 <element name="names" type="ns:ArrayOfstring" minOccurs="1" maxOccurs="1"/>
2856 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2862 Fixed-size arrays can be used to store a fixed number of values:
2867 char* names[10]; // array of 10 char* names
2872 The fixed-size array is similar to a SOAP-encoded array, which can be used with or without SOAP applications. This struct maps to a complexType that references a <i>`Array10Ofstring`</i> complexType with ten <i>`item`</i> elements:
2876 <complexType name="record">
2878 <element name="names" type="ns:Array10Ofstring" minOccurs="1" maxOccurs="1"/>
2879 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2882 <complexType name="Array10Ofstring">
2884 <element name="item" type="xsd:string" minOccurs="0" maxOccurs="10"/>
2890 @note When <b>`soapcpp2 -e`</b> option <b>`-e`</b> is used, the <i>`Array10Ofstring`</i> becomes a SOAP-encoded array for SOAP 1.1/1.2 RPC encoded messaging, see previous note.
2892 🔝 [Back to table of contents](#)
2894 ### Sequencing with hidden members {#toxsd9-10}
2896 A member becomes a hidden XML element, i.e. not visibly rendered in XML, when
2897 its name starts with a double underscore. This makes it possible to sequence a
2898 collection of data members, basically by forming a sequence of elements that
2899 can be optional or repeated together.
2901 To create a sequence of members that are optional, use a pointer-based hidden
2902 member that is a struct with the collection of members to sequence:
2907 std::string name; // required name
2908 struct __ns__optional
2910 uint64_t SSN; // SSN in optional group
2911 std::string phone; // phone number in optional group
2912 } *__optional; // optional group
2916 Here we used a hidden struct type `__ns__optional` which starts with a double
2917 underscore, because we do not want to define a new global type for the XML
2918 schema we generate. We just need a unique name for a structure that sequences
2921 This struct maps to a complexType in the soapcpp2-generated XML schema:
2925 <complexType name="record">
2927 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2928 <sequence minOccurs="0" maxOccurs="1">
2929 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2930 <element name="phone" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2937 The `name` member is a required element of the <i>`ns:record`</i> complexType.
2938 The <i>`ns:record`</i> complexType has an optional sequence of `SSN` and
2941 To create repetitions of a sequence of members, use an array as follows:
2946 std::string name; // required name
2947 $int sizeofarray; // size of group array
2950 uint64_t SSN; // SSN in group
2951 std::string phone; // phone number in group
2952 } *__array; // group array
2956 This struct maps to a complexType in the soapcpp2-generated XML schema:
2960 <complexType name="record">
2962 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2963 <sequence minOccurs="0" maxOccurs="unbounded">
2964 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2965 <element name="phone" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2972 The `name` member is a required element of the <i>`ns:record`</i> complexType.
2973 The <i>`ns:record`</i> complexType has a potentially unbounded sequence of
2974 `SSN` and `phone` elements. You can specify array bounds instead of zero to
2975 unbounded, see [Container and array members and their occurrence constraints](#toxsd9-9).
2977 The XML value space consists of a sequence of SSN and phone elements:
2982 <name>numbers</name>
2983 <SSN>1234567890</SSN>
2984 <phone>555-123-4567</phone>
2985 <SSN>1987654320</SSN>
2986 <phone>555-789-1234</phone>
2987 <SSN>2345678901</SSN>
2988 <phone>555-987-6543</phone>
2993 🔝 [Back to table of contents](#)
2995 ### Tagged union members {#toxsd9-11}
2997 A union member in a class or in a struct cannot be serialized unless a
2998 discriminating *variant selector* member is provided that tells the serializer
2999 which union field to serialize. This effectively creates a *tagged union*.
3001 The variant selector is associated with the union as a selector-union pair of members.
3002 The variant selector is a member with the name `__union` or `__unionX`, where
3003 `X` can be any name, or by an `$int` member to identify the member as a variant
3010 $int xORnORs; // variant selector with values SOAP_UNION_fieldname
3021 The variant selector values are auto-generated based on the union name `choice`
3022 and the names of its members `x`, `n`, and `s`:
3024 - `xORnORs = SOAP_UNION_ns__choice_x` when `u.x` is valid.
3026 - `xORnORs = SOAP_UNION_ns__choice_n` when `u.n` is valid.
3028 - `xORnORs = SOAP_UNION_ns__choice_s` when `u.s` is valid.
3030 - `xORnORs = 0` when none are valid (should only be used with great care,
3031 because XSD validation may fail when content is required but absent).
3033 This class maps to a complexType with a sequence and choice in the
3034 soapcpp2-generated XML schema:
3038 <complexType name="record">
3041 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
3042 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
3043 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3045 <element name="names" type="xsd:string" minOccurs="1" maxOccurs="1" nillable="true"/>
3051 An STL container or dynamic array of a union requires wrapping the variant
3052 selector and union member in a struct:
3059 struct ns__data // data with a choice of x, n, or s
3061 $int xORnORs; // variant selector with values SOAP_UNION_fieldname
3068 }> data; // vector with data
3072 and an equivalent definition with a dynamic array instead of a `std::vector`
3073 (you can use this in C with structs):
3079 $int sizeOfdata; // size of dynamic array
3080 struct ns__data // data with a choice of x, n, or s
3082 $int xORnORs; // variant selector with values SOAP_UNION_fieldname
3089 } *data; // points to the data array of length sizeOfdata
3093 This maps to two complexTypes in the soapcpp2-generated XML schema:
3097 <complexType name="data">
3099 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
3100 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
3101 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3104 <complexType name="record">
3106 <element name="data" type="ns:data" minOccurs="0" maxOccurs="unbounded"/>
3112 The XML value space consists of a sequence of item elements each wrapped in an
3117 <ns:record xmlns:ns="urn:types" ...>
3134 To remove the wrapping data element, simply rename the wrapping struct to
3135 `__ns__data` and the member to `__data` to make this member invisible to the
3136 serializer. The double underscore prefix naming convention is used for the
3137 struct name and member name. Also use a dynamic array instead of a STL
3138 container (so you can also use this approach in C with structs):
3144 $int sizeOfdata; // size of dynamic array
3145 struct __ns__data // contains choice of x, n, or s
3147 $int xORnORs; // variant selector with values SOAP_UNION_fieldname
3154 } *__data; // points to the data array of length sizeOfdata
3158 This maps to a complexType in the soapcpp2-generated XML schema:
3162 <complexType name="record">
3163 <sequence minOccurs="0" maxOccurs="unbounded">
3165 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
3166 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
3167 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3174 The XML value space consists of a sequence of <i>`<x>`</i>, <i>`<n>`</i>, and/or <i>`<s>`</i>
3179 <ns:record xmlns:ns="urn:types" ...>
3188 Please note that structs, classes, and unions are unnested by soapcpp2 (as in
3189 the C standard of nested structs and unions). Therefore, the `ns__choice`
3190 union in the `ns__record` class is redeclared at the top level despite its
3191 nesting within the `ns__record` class. This means that you will have to choose
3192 a unique name for each nested struct, class, and union.
3194 🔝 [Back to table of contents](#)
3196 ### Tagged void pointer members {#toxsd9-12}
3198 To serialize data pointed to by `void*` requires run-time type information that
3199 tells the serializer what type of data to serialize by means of a *tagged void
3200 pointer*. This type information is stored in a special type tag member of a
3201 struct/class with the name `__type` or `__typeX`, where `X` can be any name, or
3202 alternatively by an `$int` special member of any name as a type tag:
3208 $int typeOfdata; // type tag with values SOAP_TYPE_T
3209 void *data; // points to some data of type T
3213 A type tag member has nonzero values `SOAP_TYPE_T` where `T` is the name of a
3214 struct/class or the name of a primitive type, such as `int`, `std__string` (for
3215 `std::string`), `string` (for `char*`).
3217 This class maps to a complexType with a sequence in the soapcpp2-generated
3222 <complexType name="record">
3224 <element name="data" type="xsd:anyType" minOccurs="0" maxOccurs="1"/>
3230 The XML value space consists of the XML value space of the type with the
3231 addition of an <i>`xsi:type`</i> attribute to the enveloping element:
3235 <ns:record xmlns:ns="urn:types" ...>
3236 <data xsi:type="xsd:int">123</data>
3241 This <i>`xsi:type`</i> attribute is important for the receiving end to distinguish
3242 the type of data to instantiate. The receiver cannot deserialize the data
3243 without an <i>`xsd:type`</i> attribute.
3245 You can find the `SOAP_TYPE_T` name of each serializable type in the
3246 auto-generated <i>`soapStub.h`</i> file.
3248 Also all serializable C++ classes have a virtual `int T::soap_type()` member
3249 that returns their `SOAP_TYPE_T` value that you can use.
3251 When the `void*` pointer is NULL or when `typeOfdata` is zero, the data is not
3254 An STL container or dynamic array of `void*` pointers to <i>`xsd:anyType`</i> data
3255 requires wrapping the type tag and `void*` members in a struct:
3262 struct ns__data // data with an xsd:anyType item
3264 $int typeOfitem; // type tag with values SOAP_TYPE_T
3265 void *item; // points to some item of type T
3266 }> data; // vector with data
3270 and an equivalent definition with a dynamic array instead of a `std::vector`
3271 (you can use this in C with structs):
3277 $int sizeOfdata; // size of dynamic array
3278 struct ns__data // data with an xsd:anyType item
3280 $int typeOfitem; // type tag with values SOAP_TYPE_T
3281 void *item; // points to some item of type T
3282 } *data; // points to the data array of length sizeOfdata
3286 This maps to two complexTypes in the soapcpp2-generated XML schema:
3290 <complexType name="data">
3292 <element name="item" type="xsd:anyType" minOccurs="1" maxOccurs="1" nillable="true"/>
3295 <complexType name="record">
3297 <element name="data" type="ns:data" minOccurs="0" maxOccurs="unbounded"/>
3303 The XML value space consists of a sequence of item elements each wrapped in a
3308 <ns:record xmlns:ns="urn:types" ...>
3310 <item xsi:type="xsd:int">123</item>
3313 <item xsi:type="xsd:double">3.1</item>
3316 <item xsi:type="xsd:string">abc</item>
3322 To remove the wrapping data elements, simply rename the wrapping struct and
3323 member to `__data` to make this member invisible to the serializer with the
3324 double underscore prefix naming convention. Also use a dynamic array instead
3325 of a STL container (you can use this in C with structs):
3331 $int sizeOfdata; // size of dynamic array
3332 struct __data // contains xsd:anyType item
3334 $int typeOfitem; // type tag with values SOAP_TYPE_T
3335 void *item; // points to some item of type T
3336 } *__data; // points to the data array of length sizeOfdata
3340 This maps to a complexType in the soapcpp2-generated XML schema:
3344 <complexType name="record">
3345 <sequence minOccurs="0" maxOccurs="unbounded">
3346 <element name="item" type="xsd:anyType" minOccurs="1" maxOccurs="1"/>
3352 The XML value space consists of a sequence of data elements:
3356 <ns:record xmlns:ns="urn:types" ...>
3357 <item xsi:type="xsd:int">123</item>
3358 <item xsi:type="xsd:double">3.1</item>
3359 <item xsi:type="xsd:string">abc</item>
3364 Again, please note that structs, classes, and unions are unnested by soapcpp2
3365 (as in the C standard of nested structs and unions). Therefore, the `__data`
3366 struct in the `ns__record` class is redeclared at the top level despite its
3367 nesting within the `ns__record` class. This means that you will have to choose
3368 a unique name for each nested struct, class, and union.
3370 @see Section [XSD type bindings](#typemap2).
3372 🔝 [Back to table of contents](#)
3374 ### Adding get and set methods {#toxsd9-13}
3376 A public `get` method may be added to a class or struct, which will be
3377 triggered by the deserializer. This method will be invoked right after the
3378 instance is populated by the deserializer. The `get` method can be used to
3379 update or verify deserialized content. It should return `SOAP_OK` or set
3380 `soap::error` to a nonzero error code and return it.
3382 A public `set` method may be added to a class or struct, which will be
3383 triggered by the serializer. The method will be invoked just before the
3384 instance is serialized. Likewise, the `set` method should return `SOAP_OK` or
3385 set set `soap::error` to a nonzero error code and return it.
3387 For example, adding a `set` and `get` method to a class declaration:
3393 int set(struct soap*); // triggered before serialization
3394 int get(struct soap*); // triggered after deserialization
3399 To add these and othe rmethods to classes and structs with wsdl2h and
3400 <i>`typemap.dat`</i>, please see [class/struct member additions](#typemap3).
3402 🔝 [Back to table of contents](#)
3404 ### Operations on classes and structs {#toxsd9-14}
3406 The following functions/macros are generated by soapcpp2 for each type `T`,
3407 which should make it easier to send, receive, and copy XML data in C and in
3410 - `int soap_write_T(struct soap*, T*)` writes an instance of `T` to a file via
3411 file descriptor `int soap::sendfd)` or to a stream via `std::ostream
3412 *soap::os` (C++ only) or saves into a NUL-terminated string by setting
3413 `const char **soap::os` to a string pointer to be set (C only). Returns
3414 `SOAP_OK` on success or an error code, also stored in `soap::error`.
3416 - `int soap_read_T(struct soap*, T*)` reads an instance of `T` from a file via
3417 file descriptor `int soap::recvfd)` or from a stream via `std::istream
3418 *soap::is` (C++ only) or reads from a NUL-termianted string `const char
3419 *soap::is` (C only). Returns `SOAP_OK` on success or an error code, also
3420 stored in `soap::error`.
3422 - `void soap_default_T(struct soap*, T*)` sets an instance `T` to its default
3423 value, resetting members of a struct to their initial values (for classes we
3424 use method `T::soap_default`, see below). If `T` is a struct that has a
3425 `soap` pointer member to a `::soap` context then this pointer member will be
3426 set to the first argument passed to this function to initialize its `soap`
3429 - `T * soap_dup_T(struct soap*, T *dst, const T *src)` (requires <b>`soapcpp2 -Ec`</b>)
3430 deep copy `src` into `dst`, replicating all deep cycles and shared pointers
3431 when a managing `soap` context is provided as argument. When `dst` is NULL,
3432 allocates space for `dst` and returns a pointer to the allocated copy. Deep
3433 copy results in a tree when the `soap` context is NULL, but the presence of
3434 deep cycles will lead to non-termination. Use flag `SOAP_XML_TREE` with
3435 managing context to copy into a tree without cycles and pointers to shared
3436 objects. Returns `dst` or allocated copy when `dst` is NULL.
3438 - `void soap_del_T(const T*)` (requires <b>`soapcpp2 -Ed`</b>) deletes all
3439 heap-allocated members of this object by deep deletion ONLY IF this object
3440 and all of its (deep) members are not managed by a `soap` context AND the deep
3441 structure is a tree (no cycles and co-referenced objects by way of multiple
3442 (non-smart) pointers pointing to the same data). Can be safely used after
3443 `soap_dup(NULL)` to delete the deep copy. Does not delete the object itself.
3445 When in C++ mode, soapcpp2 tool adds several methods to classes in addition to
3446 adding a default constructor and destructor (when these were not explicitly
3449 The public methods added to a class `T`:
3451 - `virtual int T::soap_type(void)` returns a unique type ID (`SOAP_TYPE_T`).
3452 This numeric ID can be used to distinguish base from derived instances.
3454 - `virtual void T::soap_default(struct soap*)` sets all data members to
3455 default values. If class `T` has a `soap` pointer member to a `::soap`
3456 context then this pointer member will be set to the argument passed to this
3457 function to initialize its `soap` pointer member.
3459 - `virtual void T::soap_serialize(struct soap*) const` serializes object to
3460 prepare for SOAP 1.1/1.2 encoded output (or with `SOAP_XML_GRAPH`) by
3461 analyzing its (cyclic) structures.
3463 - `virtual int T::soap_put(struct soap*, const char *tag, const char *type) const`
3464 emits object in XML, compliant with SOAP 1.1 encoding style, return error
3465 code or `SOAP_OK`. Requires `soap_begin_send(soap)` and
3466 `soap_end_send(soap)`.
3468 - `virtual int T::soap_out(struct soap*, const char *tag, int id, const char *type) const`
3469 emits object in XML, with tag and optional id attribute and <i>`xsi:type`</i>,
3470 return error code or `SOAP_OK`. Requires `soap_begin_send(soap)` and
3471 `soap_end_send(soap)`.
3473 - `virtual void * T::soap_get(struct soap*, const char *tag, const char *type)`
3474 Get object from XML, compliant with SOAP 1.1 encoding style, return pointer
3475 to object or NULL on error. Requires `soap_begin_recv(soap)` and
3476 `soap_end_recv(soap)`.
3478 - `virtual void *soap_in(struct soap*, const char *tag, const char *type)`
3479 Get object from XML, with matching tag and type (NULL matches any tag and
3480 type), return pointer to object or NULL on error. Requires
3481 `soap_begin_recv(soap)` and `soap_end_recv(soap)`
3483 - `virtual T * T::soap_alloc(void) const` returns a new object of type `T`,
3484 default initialized and not managed by a `soap` context.
3486 - `virtual T * T::soap_dup(struct soap*) const` (requires <b>`soapcpp2 -Ec`</b>)
3487 returns a duplicate of this object by deep copying, replicating all deep
3488 cycles and shared pointers when a managing `soap` context is provided as
3489 argument. Deep copy is a tree when argument is NULL, but the presence of
3490 deep cycles will lead to non-termination. Use flag `SOAP_XML_TREE` with the
3491 managing context to copy into a tree without cycles and pointers to shared
3494 - `virtual void T::soap_del() const` (rquires <b>`soapcpp2 -Ed`</b>) deletes all
3495 heap-allocated members of this object by deep deletion ONLY IF this object
3496 and all of its (deep) members are not managed by a `soap` context AND the deep
3497 structure is a tree (no cycles and co-referenced objects by way of multiple
3498 (non-smart) pointers pointing to the same data). Can be safely used after
3499 `soap_dup(NULL)` to delete the deep copy. Does not delete the object itself.
3501 Also, there are four variations of `soap_new_T` for
3502 class/struct/template type `T` that soapcpp2 auto-generates to create instances
3503 on a context-managed heap:
3505 - `T * soap_new_T(struct soap*)` returns a new instance of `T` with default data
3506 member initializations that are set with the soapcpp2 auto-generated `void
3507 T::soap_default(struct soap*)` method), but ONLY IF the soapcpp2
3508 auto-generated default constructor is used that invokes `soap_default()` and
3509 was not replaced by a user-defined default constructor.
3511 - `T * soap_new_T(struct soap*, int n)` returns an array of `n` new instances of
3512 `T`. Similar to the above, instances are initialized.
3514 - `T * soap_new_req_T(struct soap*, ...)` returns a new instance of `T` and sets
3515 the required data members to the values specified in `...`. The required data
3516 members are those with nonzero minOccurs, see the subsections on
3517 [(smart) pointer members and their occurrence constraints](#toxsd9-8) and
3518 [container and array members and their occurrence constraints](#toxsd9-9).
3520 - `T * soap_new_set_T(struct soap*, ...)` returns a new instance of `T` and sets
3521 the public/serializable data members to the values specified in `...`.
3523 The above functions can be invoked with a NULL `soap` context, but we will be
3524 responsible to use `delete T` to remove this instance from the unmanaged heap.
3526 🔝 [Back to table of contents](#)
3528 Special classes and structs {#toxsd10}
3529 ---------------------------
3531 The following applies to both structs and classes. The examples show classes
3532 in C++. For C, use structs and omit the C++ features. Structs also require
3533 the use of the `struct` keyword, otherwise soapcpp2 will throw a syntax error.
3535 ### SOAP-encoded arrays {#toxsd10-1}
3537 A class or struct with the following layout is a one-dimensional SOAP-encoded
3544 T *__ptr; // array pointer
3545 int __size; // array size
3549 where `T` is the array element type. A multidimensional SOAP Array is:
3555 T *__ptr; // array pointer
3556 int __size[N]; // array size of each dimension
3560 where `N` is the constant number of dimensions. The pointer points to an array
3561 of `__size[0]*__size[1]* ... * __size[N-1]` elements.
3563 This maps to a complexType restriction of SOAP-ENC:Array in the
3564 soapcpp2-generated XML schema:
3568 <complexType name="ArrayOfT">
3570 <restriction base="SOAP-ENC:Array">
3572 <element name="item" type="T" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
3574 <attribute ref="SOAP-ENC:arrayType" WSDL:arrayType="ArrayOfT[]"/>
3581 The name of the class can be arbitrary. We often use `ArrayOfT` without a
3582 prefix to distinguish arrays from other classes and structs.
3584 With SOAP 1.1 encoding, an optional offset member can be added that controls
3585 the start of the index range for each dimension:
3591 T *__ptr; // array pointer
3592 int __size[N]; // array size of each dimension
3593 int __offset[N]; // array offsets to start each dimension
3597 For example, we can define a matrix of floats as follows:
3608 The following code populates the matrix and serializes it in XML:
3611 soap *soap = soap_new1(SOAP_XML_INDENT);
3613 double a[6] = { 1, 2, 3, 4, 5, 6 };
3617 soap_write_Matrix(soap, &A);
3620 Matrix A is serialized as an array with 2x3 values:
3624 <SOAP-ENC:Array SOAP-ENC:arrayType="xsd:double[2,3]" ...>
3635 🔝 [Back to table of contents](#)
3637 ### XSD hexBinary and base64Binary types {#toxsd10-2}
3639 A special case of a one-dimensional array is used to define <i>`xsd:hexBinary`</i> and
3640 <i>`xsd:base64Binary`</i> types when the pointer type is `unsigned char`:
3643 class xsd__hexBinary
3646 unsigned char *__ptr; // points to raw binary data
3647 int __size; // size of data
3654 class xsd__base64Binary
3657 unsigned char *__ptr; // points to raw binary data
3658 int __size; // size of data
3662 To create a new binary type, use either one of the following three forms that
3663 declare a new `ns__binary` type that is a <i>`simpleType`</i> restriction of
3664 <i>`xsd:base64Binary`</i>:
3667 typedef xsd__base64Binary ns__binary;
3670 class ns__binary : public xsd__base64Binary
3672 ... // attribute members (@) and class methods
3679 unsigned char *__ptr; // points to raw binary data
3680 int __size; // size of data
3681 ... // attribute members (@) and class methods (optional)
3685 Here, `xsd__base64Binary` is reused in the first two cases, where
3686 `xsd__base64Binary` is declared as shown above.
3688 @see [DIME/MIME/MTOM attachment binary types](#toxsd10-3)
3690 🔝 [Back to table of contents](#)
3692 ### DIME/MIME/MTOM attachment binary types {#toxsd10-3}
3694 A class or struct with a binary content layout can be extended to support
3695 attachments. The following struct or class type can be used as DIME, MIME, and
3696 MTOM attachment and also be used for <i>`xsd:base64Binary`</i> type values:
3699 class xsd__base64Binary
3702 unsigned char *__ptr; // points to raw binary data
3703 int __size; // size of data
3704 char *id; // NULL to generate an id, or set to a unique UUID
3705 char *type; // MIME type of the data
3706 char *options; // optional description of MIME attachment
3710 When the `id`, `type`, or `options` members are non-NULL, an attachment will be
3711 used instead of base64 XML content. DIME attachments are the default. To
3712 switch to MIME use the `SOAP_ENC_MIME` context flag. To switch to MTOM use the
3713 `SOAP_ENC_MTOM` context flag.
3715 MTOM is typically used with XOP <i>`<xop:Include>`</i> elements, which is
3716 preferred and declared as follows:
3719 //gsoap xop schema import: http://www.w3.org/2004/08/xop/include
3723 unsigned char *__ptr; // points to raw binary data
3724 int __size; // size of data
3725 char *id; // NULL to generate an id, or set to a unique UUID
3726 char *type; // MIME type of the data
3727 char *options; // optional description of MIME attachment
3731 Attachments are beyond the scope of this article. See the
3732 [gSOAP user guide.](../../guide/html/index.html) for more details.
3734 🔝 [Back to table of contents](#)
3736 ### Wrapper class/struct with simpleContent {#toxsd10-4}
3738 A class or struct with the following layout is a complexType that wraps
3745 T __item; // primitive type for the simpleContent
3746 ... // attribute members (@) and class methods (optional)
3750 The type `T` is a primitive type (`bool`, `enum`, `time_t`, numeric and string
3751 types), `xsd__hexBinary`, `xsd__base64Binary`, and custom serializers, such as
3754 This maps to a complexType with simpleContent in the soapcpp2-generated XML
3759 <complexType name="simple">
3761 <extension base="T"/>
3767 A wrapper class/struct may include any number of members that are declared as
3768 attributes with `@`, which should be placed after the `__item` member.
3770 🔝 [Back to table of contents](#)
3772 ### DOM anyType and anyAttribute {#toxsd10-5}
3774 Use of a DOM is optional and enabled by `#import "dom.h"` to use the DOM
3775 `xsd__anyType` element node and `xsd__anyAttribute` attribute node:
3783 @xsd__anyAttribute attributes; // optional DOM attributes
3784 xsd__anyType *name; // optional DOM element (pointer means minOccurs=0)
3785 xsd__anyType address; // required DOM element (minOccurs=1)
3786 xsd__anyType email 0; // optional DOM element (minOccurs=0)
3787 ... // other members
3791 where `name` contains XML stored in a DOM node set and `attributes` is a list
3792 of all visibly rendered attributes. The name `attributes` is arbitrary and any
3795 You should place the `xsd__anyType` members at the end of the struct or class.
3796 This ensures that the DOM members are populated last as a "catch all". A
3797 member name starting with double underscore is a wildcard member. These
3798 members are placed at the end of a struct or class automatically by soapcpp2.
3800 An `#import "dom.h"` import is automatically added by <b>`wsdl2h -d`</b> with
3801 option <b>`-d`</b> to bind <i>`xsd:anyType`</i> to DOM nodes, and also to
3802 populate <i>`xsd:any`</i>, <i>`xsd:anyAttribute`</i> and <i>`xsd:mixed`</i> XML
3811 @xsd__anyAttribute __anyAttribute; // optional DOM attributes
3812 std::vector<xsd__anyType> __any 0; // optional DOM elements (minOccurs=0)
3813 xsd__anyType __mixed 0; // optional mixed content (minOccurs=0)
3814 ... // other members
3818 where the members prefixed with `__` are "invisible" to the XML parser, meaning
3819 that these members are not bound to XML tag names.
3821 In C you can use a dynamic arrary instead of `std::vector`:
3828 @xsd__anyAttribute __anyAttribute; // optional DOM attributes
3829 $int __sizeOfany; // size of the array
3830 xsd__anyType *__any; // optional DOM elements (pointer means minOccurs=0)
3831 xsd__anyType __mixed 0; // optional mixed content (minOccurs=0)
3832 ... // other members
3836 Classes can inherit DOM, which enables full use of polymorphism with one base
3842 class ns__record : public xsd__anyType
3844 std::vector<xsd__anyType*> array; // array of objects of any class
3845 ... // other members
3849 This permits an `xsd__anyType` pointer to refer to a derived class such as
3850 `ns__record`, which will be serialized with an <i>`xsi:type`</i> attribute that is
3851 set to "ns:record". The <i>`xsi:type`</i> attributes add the necessary type information
3852 to distinguish the XML content from the DOM base type. This is important for
3853 the receiving end: without <i>`xsd:type`</i> attributes with type names, only base DOM
3854 objects are recognized and instantiated.
3856 Because C lacks OOP principles such as class inheritance and polymorphism, you
3857 will need to use the special [`void*` members](#toxsd9-12) to serialize data
3858 pointed to by a `void*` member.
3860 To ensure that wsdl2h generates pointer-based `xsd__anyType` DOM nodes with
3861 <b>`wsdl2h -d`</b> using option <b>`-d`</b> for <i>`xsd:any`</i>, add the
3862 following line to <i>`typemap.dat`</i>:
3864 xsd__any = | xsd__anyType*
3866 This lets wsdl2h produce class/struct members and containers with
3867 `xsd__anyType*` for <i>`xsd:any`</i> instead of `xsd__anyType`. To just force all
3868 <i>`xsd:anyType`</i> uses to be pointer-based, declare in <i>`typemap.dat`</i>:
3870 xsd__anyType = | xsd__anyType*
3872 If you use <b>`wsdl2h -d -p`</b> using options <b>`-d`</b> and <b>`-p`</b> then
3873 every class will inherit DOM as shown above. Without option `-d`, an
3874 `xsd__anyType` type is generated to serve as the root type in the type
3878 class xsd__anyType { _XML __item; struct soap *soap; };
3880 class ns__record : public xsd__anyType
3886 where the `_XML __item` member holds any XML content as a literal XML string.
3888 To use the DOM API, compile <i>`dom.c`</i> (or <i>`dom.cpp`</i> for C++), or
3889 link the gSOAP library with <b>`-lgsoapssl`</b> (or <b>`-lgsoapssl++`</b> for C++).
3891 @see Documentation of [XML DOM and XPath](http://www.genivia.com/doc/dom/html)
3894 🔝 [Back to table of contents](#)
3896 Directives {#directives}
3899 You can use `//gsoap` directives in the interface header file with the data
3900 binding interface for soapcpp2. These directives are used to configure the
3901 code generated by soapcpp2 by declaring various. properties of Web services
3902 and XML schemas. When using the wsdl2h tool, you will notice that wsdl2h
3903 generates directives automatically based on the WSDL and XSD input.
3905 Service directives are applicable to service and operations described by WSDL.
3906 Schema directives are applicable to types, elements, and attributes defined by
3909 🔝 [Back to table of contents](#)
3911 Service directives {#directives-1}
3914 A service directive must start at a new line and is of the form:
3917 //gsoap <prefix> service <property>: <value>
3920 where `<prefix>` is the XML namespace prefix of a service binding. The
3921 `<property>` and `<value>` fields are one of the following:
3924 --------------- | -----
3925 `name` | name of the service, optionally followed by text describing the service
3926 `namespace` | URI of the WSDL targetNamespace
3927 `documentation` | text describing the service (see also the `name` property), multiple permitted
3928 `doc` | an alias for the `documentation` property
3929 `style` | `document` (default) SOAP messaging style or `rpc` for SOAP RPC
3930 `encoding` | `literal` (default), `encoded` for SOAP encoding, or a custom URI
3931 `protocol` | specifies SOAP or REST, see below
3932 `port` | URL of the service endpoint, usually an http or https address, to use in the WSDL definitions/service/port/address/\@location
3933 `location` | an alias for the `port` property
3934 `endpoint` | an alias for the `port` property
3935 `transport` | URI declaration of the transport, usually `http://schemas.xmlsoap.org/soap/http`
3936 `definitions` | name of the WSDL definitions/\@name
3937 `type` | name of the WSDL definitions/portType/\@name (WSDL2.0 interface/\@name)
3938 `portType` | an alias for the `type` property (`portType` follows SOAP 1.1 naming conventions)
3939 `interface` | an alias for the `type` property (`interface` follows SOAP 1.2 naming conventions)
3940 `binding` | name of the WSDL definitions/binding/\@name
3941 `portName` | name of the WSDL definitions/service/port/\@name
3942 `executable` | name of the "executable" to use in the WSDL definitions/service/port/address/\@location
3944 The service `name` and `namespace` properties are required in order to generate
3945 a valid WSDL with soapcpp2. The other properties are optional.
3947 The `style` and `encoding` property defaults are changed with
3948 <b>`soapcpp2 -e`</b> option <b>`-e`</b> to `rpc` and `encoded`, respectively.
3950 The `protocol` property is `SOAP` by default (SOAP 1.1). Protocol property
3953 protocol value | description
3954 -------------- | -----------
3955 `SOAP` | SOAP transport, supporting both SOAP 1.1 and 1.2
3956 `SOAP1.1` | SOAP 1.1 transport (same as `soapcpp2 -1`)
3957 `SOAP1.2` | SOAP 1.2 transport (same as `soapcpp2 -2`)
3958 `SOAP-GET` | one-way SOAP 1.1 or 1.2 with HTTP GET
3959 `SOAP1.1-GET` | one-way SOAP 1.1 with HTTP GET
3960 `SOAP1.2-GET` | one-way SOAP 1.2 with HTTP GET
3961 `HTTP` | non-SOAP REST protocol with HTTP POST
3962 `POST` | non-SOAP REST protocol with HTTP POST
3963 `GET` | non-SOAP REST protocol with HTTP GET
3964 `PUT` | non-SOAP REST protocol with HTTP PUT
3965 `DELETE` | non-SOAP REST protocol with HTTP DELETE
3967 You can bind service operations to the WSDL namespace of a service by using the
3968 namespace prefix as part of the identifier name of the function that defines
3969 the service operation:
3972 int prefix__func(arg1, arg2, ..., argn, result);
3975 You can override the `port` endpoint URL at runtime in the auto-generated
3976 `soap_call_prefix__func` service call (C/C++ client side) and in the C++ proxy
3979 🔝 [Back to table of contents](#)
3981 Service method directives {#directives-2}
3982 -------------------------
3984 Service properties are applicable to a service and to all of its operations.
3985 Service method directives are specifically applicable to a service operation.
3987 A service method directive is of the form:
3990 //gsoap <prefix> service method-<property>: <method> <value>
3993 where `<prefix>` is the XML namespace prefix of a service binding and
3994 `<method>` is the unqualified name of a service operation. The `<property>`
3995 and `<value>` fields are one of the following:
3997 method property | value
3998 --------------------------- | -----
3999 `method-documentation` | text describing the service operation
4000 `method` | an alias for the `method-documentation` property
4001 `method-action` | `""` or URI SOAPAction HTTP header, or URL query string for REST protocols
4002 `method-input-action` | `""` or URI SOAPAction HTTP header of service request messages
4003 `method-output-action` | `""` or URI SOAPAction HTTP header of service response messages
4004 `method-fault-action` | `""` or URI SOAPAction HTTP header of service fault messages
4005 `method-header-part` | member name of the `SOAP_ENV__Header` struct used in SOAP Header
4006 `method-input-header-part` | member name of the `SOAP_ENV__Header` struct used in SOAP Headers of requests
4007 `method-output-header-part` | member name of the `SOAP_ENV__Header` struct used in SOAP Headers of responses
4008 `method-fault` | type name of a struct or class member used in `SOAP_ENV__Details` struct
4009 `method-mime-type` | REST content type or SOAP MIME attachment content type(s)
4010 `method-input-mime-type` | REST content type or SOAP MIME attachment content type(s) of request message
4011 `method-output-mime-type` | REST content type or SOAP MIME attachment content type(s) of response message
4012 `method-style` | `document` or `rpc`
4013 `method-encoding` | `literal`, `encoded`, or a custom URI for encodingStyle of messages
4014 `method-response-encoding` | `literal`, `encoded`, or a custom URI for encodingStyle of response messages
4015 `method-protocol` | SOAP or REST, see [service directives](#directives-1)
4017 The `method-header-part` properties can be repeated for a service operation to
4018 declare multiple SOAP Header parts that the service operation requires. You
4019 can use `method-input-header-part` and `method-output-header-part` to
4020 differentiate between request and response messages.
4022 The `method-fault` property can be repeated for a service operation to declare
4023 multiple faults that the service operation may return.
4025 The `method-action` property serves two purposes:
4027 -# To set the SOAPAction header for SOAP protocols, i.e. sets the
4028 definitions/binding/operation/SOAP:operation/\@soapAction.
4030 -# To set the URL query string for endpoints with REST protocols, i.e. sets the
4031 definitions/binding/operation/HTTP:operation/\@location, which specifies
4032 a URL query string (starts with a `?`) to complete the service endpoint URL
4033 or extends the endpoint URL with a local path (starts with a `/`).
4035 Use `method-input-action` and `method-output-action` to differentiate the
4036 SOAPAction between SOAP request and response messages.
4038 You can always override the port endpoint URL and action values at runtime in
4039 the auto-generated `soap_call_prefix__func` service call (C/C++ client side)
4040 and in the auto-generated C++ proxy class service calls. A runtime NULL
4041 endpoint URL and/or action uses the defaults set by these directives.
4043 The `method-mime-type` property serves two purposes:
4045 -# To set the type of MIME/MTOM attachments used with SOAP protocols. Multiple
4046 attachment types can be declared for a SOAP service operation, i.e. adds
4047 definitions/binding/operation/input/MIME:multipartRelated/MIME:part/MIME:content/\@type
4048 for each type specified.
4050 -# To set the MIME type of a REST operation. This replaces XML declared in
4051 WSDL by definitions/binding/operation/(input|output)/MIME:mimeXml with
4052 MIME:content/\@type. Use `application/x-www-form-urlencoded` with REST POST
4053 and PUT protocols to send encoded form data automatically instead of XML.
4054 Only primitive type values can be transmitted with form data, such as
4055 numbers and strings, i.e. only types that are legal to use as
4056 [attributes members](#toxsd9-5).
4058 Use `method-input-mime-type` and `method-output-mime-type` to differentiate the
4059 attachment types between request and response messages.
4061 🔝 [Back to table of contents](#)
4063 Schema directives {#directives-3}
4066 A schema directive is of the form:
4069 //gsoap <prefix> schema <property>: <value>
4072 where `<prefix>` is the XML namespace prefix of a schema. The `<property>` and
4073 `<value>` fields are one of the following:
4076 --------------- | -----
4077 `namespace` | URI of the XSD targetNamespace
4078 `namespace2` | alternate URI pattern for the XSD namespace (i.e. URI is also accepted by the XML parser)
4079 `import` | URI of an imported namespace, as an alternative or in addition to `namespace`, adds `xsd:import` to the generated WSDL and XSD files
4080 `form` | `unqualified` (default) or `qualified` local element and attribute form defaults
4081 `elementForm` | `unqualified` (default) or `qualified` local element form default
4082 `attributeForm` | `unqualified` (default) or `qualified` local attribute form default
4083 `typed` | `no` (default) or `yes` for serializers to add `xsi:type` attributes to XML
4085 To learn more about the local form defaults, see [qualified and unqualified members.](#toxsd9-6)
4087 The `namespace2` URI is a pattern with `*` matching any sequence of characters
4088 and `-` matching any character. This pattern instructs the XML parser and validator
4089 to also accept the URI pattern as a valid namespace for the specified `<prefix>`.
4091 The `typed` property is implicitly `yes` when <b>`soapcpp2 -t`</b> option <b>`-t`</b> is used.
4093 🔝 [Back to table of contents](#)
4095 Schema type directives {#directives-4}
4096 ----------------------
4098 A schema type directive is of the form:
4101 //gsoap <prefix> schema type-<property>: <name> <value>
4102 //gsoap <prefix> schema type-<property>: <name>::<member> <value>
4105 where `<prefix>` is the XML namespace prefix of a schema and `<name>` is an
4106 unqualified name of a C/C++ type, and the optional `<member>` is a class/struct
4107 members or enum constant.
4109 You can describe a type with one of the following:
4111 type property | value
4112 -------------------- | -----
4113 `type-documentation` | text describing the schema type
4114 `type` | an alias for the `type-documentation` property
4116 For example, you can add a description to an enumeration:
4119 //gsoap ns schema type: Vowels The letters A, E, I, O, U, and sometimes Y
4120 //gsoap ns schema type: Vowels::Y A vowel, sometimes
4121 enum class ns__Vowels : char { A = 'A', E = 'E', I = 'I', O = 'O', U = 'U', Y = 'Y' };
4124 This documented enumeration maps to a simpleType restriction of <i>`xsd:string`</i> in
4125 the soapcpp2-generated schema:
4129 <simpleType name="Vowels">
4131 <documentation>The letters A, E, I, O, U, and sometimes Y</documentation>
4133 <restriction base="xsd:string">
4134 <enumeration value="A"/>
4135 <enumeration value="E"/>
4136 <enumeration value="I"/>
4137 <enumeration value="O"/>
4138 <enumeration value="U"/>
4139 <enumeration value="Y">
4141 <documentation>A vowel, sometimes</documentation>
4149 🔝 [Back to table of contents](#)
4151 Serialization rules {#rules}
4154 A presentation on XML data bindings is not complete without discussing the
4155 serialization rules and options that put your data in XML on the wire or store
4156 it a file or buffer.
4158 There are several options to choose from to serialize data in XML. The choice
4159 depends on the use of the SOAP protocol or if SOAP is not required. The wsdl2h
4160 tool automates this for you by taking the WSDL transport bindings into account
4161 when generating the service functions in C and C++ that use SOAP or REST.
4163 The gSOAP tools are not limited to SOAP. The tools implement generic XML data
4164 bindings for SOAP, REST, and other uses of XML. So you can read and write XML
4165 using the serializing [operations on classes and structs](#toxsd9-14).
4167 The following sections briefly explain the serialization rules with respect to
4168 the SOAP protocol for XML Web services. A basic understanding of the SOAP
4169 protocol is useful when developing client and server applications that must
4170 interoperate with other SOAP applications.
4172 SOAP/REST Web service client and service operations are represented as
4173 functions in your interface header file with the data binding interface for
4174 soapcpp2. The soapcpp2 tool will translate these function to client-side
4175 service invocation calls and server-side service operation dispatchers.
4177 A discussion of SOAP clients and servers is beyond the scope of this article.
4178 However, the SOAP options discussed here also apply to SOAP client and server
4181 🔝 [Back to table of contents](#)
4183 SOAP document versus rpc style {#doc-rpc}
4184 ------------------------------
4186 The `wsdl:binding/soap:binding/@style` attribute in the <i>`<wsdl:binding>`</i>
4187 section of a WSDL is either "document" or "rpc". The "rpc" style refers to
4188 SOAP RPC (Remote Procedure Call), which is more restrictive than the "document"
4189 style by requiring one XML element in the SOAP Body to act as the procedure
4190 name with XML subelements as its parameters.
4192 For example, the following directives in the interface header file for soapcpp2
4193 declare that `DBupdate` is a SOAP RPC encoding service method:
4196 //gsoap ns service namespace: urn:DB
4197 //gsoap ns service method-protocol: DBupdate SOAP
4198 //gsoap ns service method-style: DBupdate rpc
4199 int ns__DBupdate(...);
4202 The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with
4203 one element representing the operation with the parameters as subelements:
4208 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
4209 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
4210 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4211 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4218 </SOAP-ENV:Envelope>
4222 The "document" style puts no restrictions on the SOAP Body content. However, we
4223 recommend that the first element's tag name in the SOAP Body should be unique
4224 to each type of operation, so that the receiver can dispatch the operation
4225 based on this element's tag name. Alternatively, the HTTP URL path can be used
4226 to specify the operation, or the HTTP action header can be used to dispatch
4227 operations automatically on the server side (soapcpp2 options -a and -A).
4229 🔝 [Back to table of contents](#)
4231 SOAP literal versus encoding {#lit-enc}
4232 ----------------------------
4234 The `wsdl:operation/soap:body/@use` attribute in the <i>`<wsdl:binding>`</i> section
4235 of a WSDL is either "literal" or "encoded". The "encoded" use refers to the
4236 SOAP encoding rules that support id-ref multi-referenced elements to serialize
4239 SOAP encoding is very useful if the data internally forms a graph (including
4240 cycles) and we want the graph to be serialized in XML in a format that ensures
4241 that its structure is preserved. In that case, SOAP 1.2 encoding is the best
4244 SOAP encoding also adds encoding rules for [SOAP arrays](#toxsd10) to serialize
4245 multi-dimensional arrays. The use of XML attributes to exchange XML data in
4246 SOAP encoding is not permitted. The only attributes permitted are the standard
4247 XSD attributes, SOAP encoding attributes (such as for arrays), and id-ref.
4249 For example, the following directives in the interface header file for soapcpp2
4250 declare that `DBupdate` is a SOAP RPC encoding service method:
4253 //gsoap ns service namespace: urn:DB
4254 //gsoap ns service method-protocol: DBupdate SOAP
4255 //gsoap ns service method-style: DBupdate rpc
4256 //gsoap ns service method-encoding: DBupdate encoded
4257 int ns__DBupdate(...);
4260 The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with
4261 an encodingStyle attribute for SOAP 1.1 encoding and an element representing the
4262 operation with parameters that are SOAP 1.1 encoded:
4267 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
4268 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
4269 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4270 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4272 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
4274 <records SOAP-ENC:arrayType="ns:record[3]">
4277 <SSN>1234567890</SSN>
4281 <SSN>1987654320</SSN>
4285 <SSN>2345678901</SSN>
4289 <id id="_1" xsi:type="xsd:string">Joe</id>
4291 </SOAP-ENV:Envelope>
4295 In the XML fragment shown above the name "Joe" is shared by two records and the
4296 string is referenced by SOAP 1.1 href and id attributes.
4298 While the soapcpp-generated serializers only introduce multi-referenced
4299 elements in the payload when they are actually multi-referenced in the data
4300 graph, other SOAP applications may render multi-referenced elements more
4301 aggressively. The example could also be rendered as:
4306 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
4307 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
4308 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4309 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4311 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
4313 <records SOAP-ENC:arrayType="ns:record[3]">
4319 <id id="id1" xsi:type="ns:record">
4321 <SSN>1234567890</SSN>
4323 <id id="id2" xsi:type="ns:record">
4325 <SSN>1987654320</SSN>
4327 <id id="id3" xsi:type="ns:record">
4329 <SSN>2345678901</SSN>
4331 <id id="id4" xsi:type="xsd:string">Joe</id>
4332 <id id="id5" xsi:type="xsd:string">Jane</id>
4334 </SOAP-ENV:Envelope>
4338 SOAP 1.2 encoding is cleaner and produces more accurate XML encodings of data
4339 graphs by setting the id attribute on the element that is referenced:
4344 xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
4345 xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
4346 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4347 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4350 <ns:DBupdate SOAP-ENV:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
4351 <records SOAP-ENC:itemType="ns:record" SOAP-ENC:arraySize="3">
4353 <name SOAP-ENC:id="_1">Joe</name>
4354 <SSN>1234567890</SSN>
4358 <SSN>1987654320</SSN>
4361 <name SOAP-ENC:ref="_1"/>
4362 <SSN>2345678901</SSN>
4367 </SOAP-ENV:Envelope>
4371 @note Some SOAP 1.2 applications consider the namespace `SOAP-ENC` of
4372 <i>`SOAP-ENC:id`</i> and <i>`SOAP-ENC:ref`</i> optional. With gSOAP, the SOAP
4373 1.2 encoding serialization follows the 2007 standard, while accepting
4374 unqualified id and ref attributes.
4376 To remove all rendered id-ref multi-referenced elements, use the
4377 `SOAP_XML_TREE` flag to initialize the `soap` context.
4379 Some XSD validation rules are turned off with SOAP encoding, because of the
4380 presence of additional attributes, such as id and ref/href, SOAP arrays with
4381 arbitrary element tags for array elements, and the occurrence of additional
4382 multi-ref elements in the SOAP 1.1 Body.
4384 The use of "literal" puts no restrictions on the XML in the SOAP Body. Full
4385 XSD validation is possible, which can be enabled with the `SOAP_XML_STRICT`
4386 flag to initialize the `soap` context. However, data graphs will be
4387 serialized as trees and cycles in the data will be cut from the XML rendition.
4389 🔝 [Back to table of contents](#)
4391 SOAP 1.1 versus SOAP 1.2 {#soap}
4392 ------------------------
4394 There are two SOAP protocol versions: 1.1 and 1.2. The gSOAP tools can switch
4395 between the two versions seamlessly. You can declare the default SOAP version
4396 for a service operation as follows:
4399 //gsoap ns service method-protocol: DBupdate SOAP1.2
4402 Use `SOAP` (SOAP 1.1), `SOAP1.1`, `SOAP1.2`, and `HTTP` to switch SOAP versions
4403 or enable REST methods with HTTP POST. See [Service directives](#directives-1)
4404 and [XML serialization](#non-soap).
4406 The soapcpp2 tool auto-generates client and server code. At the client side,
4407 this operation sends data with SOAP 1.2 but accepts responses also in SOAP 1.1.
4408 At the server side, this operation accepts requests in SOAP 1.1 and 1.2 and
4409 will return responses in the same SOAP version.
4411 As we discussed in the previous section, the SOAP 1.2 protocol has a cleaner
4412 multi-referenced element serialization format that greatly enhances the
4413 accuracy of data graph serialization with SOAP RPC encoding and is therefore
4416 The SOAP 1.2 protocol default can also be set by importing and loading
4417 <i>`gsoap/import/soap12.h`</i>:
4423 Finally, the soapcpp2 tool has options to force SOAP 1.1, SOAP 1.2, or remove
4424 SOAP altogether with <b>`soapcpp2 -1`</b> (SOAP 1.1), <b>`soapcpp2 -2`</b>
4425 (SOAP 1.2) and <b>`soapcpp2 -0`</b> (plain XML, no SOAP).
4427 🔝 [Back to table of contents](#)
4429 XML serialization {#non-soap}
4432 You can serialize data to XML that is stored on the heap, on the stack (locals), and
4433 static data as long as the serializable (i.e. non-transient) values are
4434 properly initialized and pointers in the data structures are either NULL or
4435 point to valid structures.
4437 When XML is deserialized into data, the data is put on the heap and managed by the
4438 `::soap` context, see also [memory management](#memory).
4440 You can read and write XML directly to a file or stream with the serializing
4441 [operations on classes and structs](#toxsd9-14).
4443 To define and use XML Web service client and service operations, we can declare
4444 these operations in your interface header file with the data binding interface
4445 for soapcpp2 as functions. The function are translated by soapcpp2 to
4446 client-side service invocation calls and server-side service operation
4449 The REST operations POST, GET, and PUT are declared with `//gsoap` directives
4450 in the interface header file for soapcpp2. For example, a REST HTTP POST
4451 operation is declared as follows:
4454 //gsoap ns service namespace: urn:DB
4455 //gsoap ns service method-protocol: DBupdate POST
4456 int ns__DBupdate(...);
4459 There are no SOAP Envelope and SOAP Body elements in the payload for
4460 `DBupdate`. Also the XML serialization rules are identical to SOAP
4461 document/literal, meaning no SOAP RPC encoding XML structures are implicitly
4462 used. The XML payload only has the operation name as an element with its
4463 parameters serialized as subelements:
4467 <ns:DBupdate xmln:ns="urn:DB" ...>
4473 To force id-ref serialization with REST similar to SOAP 1.2 multi-reference
4474 encoding, use the `SOAP_XML_GRAPH` flag to initialize the `soap` context.
4475 The XML serialization includes id and ref attributes for multi-referenced
4476 elements as follows:
4480 <ns:DBupdate xmln:ns="urn:DB" ...>
4483 <name id="_1">Joe</name>
4484 <SSN>1234567890</SSN>
4488 <SSN>1987654320</SSN>
4492 <SSN>2345678901</SSN>
4499 🔝 [Back to table of contents](#)
4501 Input and output {#io}
4504 Reading and writing XML from/to files, streams and string buffers is done via
4505 the managing `soap` context by setting one of the following context variables that
4506 control IO sources and sinks:
4509 soap->recvfd = fd; // an int file descriptor to read from (0 by default)
4510 soap->sendfd = fd; // an int file descriptor to write to (1 by default)
4511 soap->is = &is; // C++ only: a std::istream is object to read from
4512 soap->os = &os; // C++ only: a std::ostream os object to write to
4513 soap->is = cs; // C only: a const char* string to read from (soap->is will advance)
4514 soap->os = &cs; // C only: pointer to a const char*, will be set to point to the string output
4517 Normally, all of these context variables are NULL, which is required to send and
4518 receive data over sockets by gSOAP client and server applications. Therefore,
4519 if you set any of these context variables in a client or server application
4520 then you should reset them to NULL to ensure that socket communications are not
4523 @note The use of `soap::is` and `soap::os` in C requires gSOAP 2.8.28 or greater.
4525 In the following sections, we present more details on how to read and write to
4526 files and streams, and use string buffers as sources and sinks for XML data.
4528 In addition, you can set IO callback functions to handle IO at a lower level.
4529 For more details on defining your own callback functions, see the
4530 [gSOAP user guide.](../../guide/html/index.html)
4532 🔝 [Back to table of contents](#)
4534 Reading and writing from/to files and streams {#io1}
4535 ---------------------------------------------
4537 The default IO is standard input and output. Other sources and sinks (those
4538 listed above) will be used until you (re)set them. For example with file-based
4542 FILE *fp = fopen("record.xml", "r");
4545 soap->recvfd = fileno(fp); // get file descriptor of file to read from
4546 if (soap_read_ns__record(soap, &pers1))
4547 ... // handle IO error
4549 soap->recvfd = 0; // read from stdin, or -1 to block reading
4552 FILE *fp = fopen("record.xml", "w");
4555 soap->sendfd = fileno(fp); // get file descriptor of file to write to
4556 if (soap_write_ns__record(soap, &pers1))
4557 ... // handle IO error
4559 soap->sendfd = 1; // write to stdout, or -1 to block writing
4563 Similar code with streams in C++:
4569 fs.open("record.xml", std::ios::in);
4573 if (soap_read__ns__record(soap, &pers1))
4574 ... // handle IO error
4579 fs.open("record.xml", std::ios::out);
4583 if (soap_write__ns__record(soap, &pers1))
4584 ... // handle IO error
4590 🔝 [Back to table of contents](#)
4592 Reading and writing from/to string buffers {#io2}
4593 ------------------------------------------
4595 For C++ we recommend to use `std::stringstream` objects from the
4596 <i>`sstream`</i> C++ library as illustrated in the following example:
4601 std::stringstream ss;
4602 ss.str("..."); // XML to parse
4604 if (soap_read__ns__record(soap, &pers1))
4605 ... // handle IO error
4609 if (soap_write__ns__record(soap, &pers1))
4610 ... // handle IO error
4612 std::string s = ss.str(); // string with XML
4615 For C we can use `soap::is` and `soap::os` to point to strings of XML content
4616 as follows (this requires gSOAP 2.8.28 or later):
4619 soap->is = "..."; // XML to parse
4620 if (soap_read__ns__record(soap, &pers1))
4621 ... // handle IO error
4624 const char *cs = NULL;
4626 if (soap_write__ns__record(soap, &pers1))
4627 ... // handle IO error
4629 ... = cs; // string with XML (do not free(cs): managed by the context and freed with soap_end())
4632 The type of `soap::os` is a pointer to a `const char*` string. The pointer is
4633 set by the managing `soap` context to point to the XML data that is stored on
4634 the context-managed heap.
4636 For earlier gSOAP versions we recommend to use IO callbacks `soap::frecv` and
4637 `soap::fsend`, see the [gSOAP user guide.](../../guide/html/index.html).
4639 🔝 [Back to table of contents](#)
4641 Memory management {#memory}
4644 Memory management with the `soap` context enables us to allocate data in
4645 context-managed heap space that can be collectively deleted. All deserialized
4646 data is placed on the context-managed heap by the `soap` context of the engine.
4648 🔝 [Back to table of contents](#)
4650 Memory management in C {#memory1}
4651 ----------------------
4653 When working with gSOAP in C (i.e. using <b>`wsdl2h -c`</b> option <b>`-c`</b>
4654 or <b>`soapcpp2 -c`</b> option <b>`-c`</b>), data is allocated on the managed heap with:
4656 - `void *soap_malloc(struct soap*, size_t len)`.
4658 You can also make shallow copies of data with `soap_memdup` that uses
4659 `soap_malloc` and a safe version of `memcpy` to copy a chunk of data `src` with
4660 length `len` to the context-managed heap:
4662 - `void * soap_memdup(struct soap*, const void *src, size_t len)`
4664 This function returns a pointer to the copy. This function requires gSOAP
4667 In gSOAP 2.8.35 and greater versions, you can use an auto-generated function to
4668 allocate and initialize data of type `T` on the managed heap:
4670 - `T * soap_new_T(struct soap*, int n)`
4672 This function returns an array of length `n` of type `T` data that is default
4673 initialized (by internally calling `soap_malloc(soap, n * sizeof(T))` and then
4674 `soap_default_T(soap, T*)` on each array value). Use a negative value or `n=1`
4675 to allocate and initialize a single value.
4677 The `soap_malloc` function is a wrapper around `malloc`, but which also permits
4678 the `soap` context to track all heap allocations for collective deletion
4679 with `soap_end(soap)`:
4685 struct soap *soap = soap_new(); // new context
4687 struct ns__record *record = (struct ns__record*)soap_malloc(soap, sizeof(struct ns__record));
4688 soap_default_ns__record(soap, record); // auto-generated struct initializer
4690 soap_destroy(soap); // only for C++, see section on C++ below
4691 soap_end(soap); // delete record and all other heap allocations
4692 soap_free(soap); // delete context
4695 All data on the managed heap is mass-deleted with `soap_end(soap)` which must
4696 be called before `soap_done(soap)` or `soap_free(soap)`, which end the use of
4697 the `soap` context and free the context, respectively. Use
4698 `soap_free(soap)` only when the context is allocated with `soap_new()`. Use
4699 `soap_done(soap)` only when the context is stack allocated (so cannot be
4700 deleted from the heap).
4702 The managed heap is checked for memory leaks at run time when the source code
4703 is compiled with option <b>`-DDEBUG`</b>.
4705 The soapcpp2 auto-generated deserializers in C use `soap_malloc` to allocate
4706 and populate deserialized structures, which are managed by the context for
4707 collective deletion.
4709 To make `char*` and `wchar_t*` string copies to the context-managed heap, we
4710 can use the functions:
4712 - `char *soap_strdup(struct soap*, const char *str)` and
4714 - `wchar_t *soap_wstrdup(struct soap*, const wchar_t *wstr)`.
4716 If your C compiler supports `typeof` then you can use the following macro to
4717 simplify the managed heap allocation and initialization of primitive values:
4720 #define soap_assign(soap, lhs, rhs) (*(lhs = (typeof(lhs))soap_malloc(soap, sizeof(*lhs))) = rhs)
4723 Pointers to primitive values are often used for optional members. For example,
4724 assume we have the following struct:
4729 const char *name 1; // required (minOccurs=1)
4730 uint64_t *SSN; // optional (pointer means minOccurs=0)
4731 struct ns__record *spouse; // optional (pointer means minOccurs=0)
4735 Use `soap_assign` to create a SSN value on the managed heap:
4738 struct soap *soap = soap_new(); // new context
4740 struct ns__record *record = (struct ns__record*)soap_malloc(soap, sizeof(struct ns__record));
4741 soap_default_ns__record(soap, record);
4742 record->name = soap_strdup(soap, "Joe");
4743 soap_assign(soap, record->SSN, 1234567890UL);
4745 soap_end(soap); // delete managed soap_malloc'ed heap data
4746 soap_free(soap); // delete context
4749 Without the `soap_assign` macro, you will need two lines of code, one to
4750 allocate and one to assign (you should also use this if your system can run out
4754 assert((record->SSN = (uint64_t*)soap_malloc(soap, sizeof(utint64_t))) != NULL);
4755 *record->SSN = 1234567890UL;
4758 The serializer can serialize any heap, stack, or static allocated data. So we
4759 can also create a new record as follows:
4762 struct soap *soap = soap_new(); // new context
4764 struct ns__record *record = (struct ns__record*)soap_malloc(soap, sizeof(struct ns__record));
4765 static uint64_t SSN = 1234567890UL;
4766 soap_default_ns__record(soap, record);
4767 record->name = "Joe";
4768 record->SSN = &SSN; // safe to use static values: the value of record->SSN is never changed
4770 soap_end(soap); // delete managed soap_malloc'ed heap data
4771 soap_free(soap); // delete context
4774 Use the soapcpp2 auto-generated `soap_dup_T` functions to duplicate data into
4775 another `soap` context (this requires <b>`soapcpp2 -Ec`</b> option <b>`-Ec`</b> to
4776 generate), here shown for C with the second argument `dst` NULL because we want
4777 to allocate a new managed structure:
4780 struct soap *other_soap = soap_new(); // another context
4781 struct ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
4783 soap_destroy(other_soap); // only for C++, see section on C++ below
4784 soap_end(other_soap); // delete other_record and all of its deep data
4785 soap_free(other_soap); // delete context
4788 The only reason to use another `soap` context and not to use the primary `soap`
4789 context is when the primary context must be destroyed together with all of the
4790 objects it manages while some of the objects must be kept alive. If the
4791 objects that are kept alive contain deep cycles then this is the only option we
4792 have, because deep copy with a managing `soap` context detects and preserves
4793 these cycles unless the `SOAP_XML_TREE` flag is used with the `soap` context:
4796 struct soap *other_soap = soap_new1(SOAP_XML_TREE); // another context
4797 struct ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
4800 The resulting deep copy will be a full copy of the source data structure as a
4801 tree without co-referenced data (i.e. no digraph) and without cycles. Cycles
4802 are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
4804 You can also deep copy into unmanaged space and use the auto-generated
4805 `soap_del_T()` function (requires <b>`soapcpp2 -Ed`</b> option <b>`-Ed`</b> to generate) to delete
4809 struct ns__record *other_record = soap_dup_ns__record(NULL, NULL, record);
4811 soap_del_ns__record(other_record); // deep delete record data members
4812 free(other_record); // delete the record
4815 But you should not do this for any data that has deep cycles in its runtime
4816 data structure. Cycles in the data structure will lead to non-termination when
4817 making unmanaged deep copies. Consider for example:
4822 const char *name 1; // required (minOccurs=1)
4823 uint64_t SSN; // required (non-pointer means minOccurs=1)
4824 struct ns__record *spouse; // optional (pointer means minOccurs=0)
4828 The code to populate a structure with a mutual spouse relationship:
4831 struct soap *soap = soap_new();
4833 struct ns__record pers1, pers2;
4834 soap_default_ns__record(soap, &pers1);
4835 soap_default_ns__record(soap, &pers2);
4836 pers1.name = "Joe"; // OK to serialize static data
4837 pers1.SSN = 1234567890;
4838 pers1.spouse = &pers2;
4839 pers2.name = soap_strdup(soap, "Jane"); // allocates and copies a string
4840 pers2.SSN = 1987654320;
4841 pers2.spouse = &pers1;
4843 struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
4844 struct ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
4845 soap_set_mode(soap, SOAP_XML_TREE);
4846 struct ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
4849 The bad case is where there is no context used in the first argument. The deep
4850 copy functions use a context to keep track of co-referenced data nodes and
4851 cycles in the data structure copies, to copy co-referenced nodes just once.
4852 Co-references in a data structure are formed by pointers and smart pointers
4853 such as `std::shared_ptr`, such that at least two pointers point to the same
4856 The serializer can serialize any heap, stack, or static allocated data, such as
4857 in the code shown above. So we can serialize the stack-allocated `pers1`
4861 FILE *fp = fopen("record.xml", "w");
4864 soap->sendfd = fileno(fp); // file descriptor to write to
4865 soap_set_mode(soap, SOAP_XML_GRAPH); // support id-ref w/o requiring SOAP
4866 soap_clr_mode(soap, SOAP_XML_TREE); // if set, clear
4867 soap_write_ns__record(soap, &pers1);
4869 soap->sendfd = -1; // block further writing
4873 which produces an XML document record.xml that is similar to:
4877 <ns:record xmlns:ns="urn:types" id="Joe">
4879 <SSN>1234567890</SSN>
4882 <SSN>1987654320</SSN>
4883 <spouse ref="#Joe"/>
4889 Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph
4890 leads to the same non-termination problem when we later try to copy the data
4891 into unmanaged memory heap space:
4894 struct soap *soap = soap_new1(SOAP_XML_GRAPH); // support id-ref w/o SOAP
4896 struct ns__record pers1;
4897 FILE *fp = fopen("record.xml", "r");
4900 soap->recvfd = fileno(fp);
4901 if (soap_read_ns__record(soap, &pers1))
4902 ... // handle IO error
4904 soap->recvfd = -1; // blocks further reading
4907 struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
4908 struct ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
4909 soap_set_mode(soap, SOAP_XML_TREE);
4910 struct ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
4913 Copying data with `soap_dup_T(soap)` into managed heap memory space is always
4914 safe. Copying into unmanaged heap memory space requires diligence. But
4915 deleting unmanaged data is easy with `soap_del_T()`.
4917 You can also use `soap_del_T()` to delete structures that you created in C, but
4918 only if these structures are created with `malloc` and do NOT contain pointers
4919 to stack and static data.
4921 You can unlink one or more allocated objects from the managed heap to allow the
4922 object to live after `soap_end(soap)` by using:
4924 - `void soap_unlink(struct soap *soap, void *ptr)`
4926 The unlinked heap-allocated data pointed to by `ptr` can be accessed after
4927 `soap_end(soap)`. Do not forget to free the data with `free(ptr)`. Be aware
4928 that `soap_unlink(soap, ptr)` does not perform a deep unlinkage. If `ptr` is a
4929 struct, pointer members will become invalid when pointing to objects on the
4930 managed heap. Use `soap_unlink(soap, ptr->member)` to unlink `member` as well.
4932 Finally, when data is allocated in managed memory heap space, either explicitly
4933 with the allocation functions shown above or by the soapcpp2-generated
4934 deserializers, you can delegate the management and deletion of this data to
4935 another `soap` context. That context will be responsible to delete the data
4936 with `soap_end(soap)` later:
4938 - `void delegate_deletion(struct soap *soap_from, struct soap *soap_to)`
4940 This allows the `soap_from` context to be deleted with `soap_free(soap_from)`
4941 (assuming it is allocated with `soap_new()`, use `soap_done(soap_from)` when
4942 `soap_from` is stack-allocated) while the managed data remains intact. You
4943 can use this function any time, to delegate management and deletion to another
4944 context `soap_to` and then continue with the current context. You can also use
4945 different source `soap_from` contexts to delegate management and deletion to
4946 the other `soap_to` context. To mass delete all managed data, use
4947 `soap_end(soap_to)`.
4949 🔝 [Back to table of contents](#)
4951 Memory management in C++ {#memory2}
4952 ------------------------
4954 When working with gSOAP in C++, the engine allocates data on a managed heap
4955 using `soap_new_T(soap)` to allocate a type with type name `T`. Managed heap
4956 allocation is tracked by the `soap` context for collective deletion with
4957 `soap_destroy(soap)` for structs, classes, and templates and with
4958 `soap_end(soap)` for everything else.
4960 You should only use `soap_malloc(struct soap*, size_t len)` to allocate
4961 primitive types, but `soap_new_T` is preferred. The auto-generated `T *
4962 soap_new_T(struct soap*)` returns data allocated on the managed heap for type
4963 `T`. The data is mass-deleted with `soap_destroy(soap)` followed by
4966 The `soap_new_T` functions return NULL when allocation fails. C++ exceptions
4967 are never raised by the engine and serializers when data is allocated, unless
4968 `SOAP_NOTHROW` (set to `(std::nothrow)`) is redefined to permit `new` to throw
4971 There are four variations of `soap_new_T` functions to allocate data of type
4972 `T` that soapcpp2 auto-generates:
4974 - `T * soap_new_T(struct soap*)` returns a new instance of `T` that is default
4975 initialized. For classes, initialization is internally performed using the
4976 soapcpp2 auto-generated `void T::soap_default(struct soap*)` method of the
4977 class, but ONLY IF the soapcpp2 auto-generated default constructor is used
4978 that invokes `soap_default()` and was not replaced by a user-defined default
4981 - `T * soap_new_T(struct soap*, int n)` returns an array of `n` new instances of
4982 `T`. The instances in the array are default initialized as described above.
4984 - `T * soap_new_req_T(struct soap*, ...)` (structs and classes only) returns a
4985 new instance of `T` and sets the required data members to the values
4986 specified in `...`. The required data members are those with nonzero
4987 minOccurs, see the subsections on
4988 [(smart) pointer members and their occurrence constraints](#toxsd9-8) and
4989 [container and array members and their occurrence constraints](#toxsd9-9).
4991 - `T * soap_new_set_T(struct soap*, ...)` (structs and classes only) returns a
4992 new instance of `T` and sets the public/serializable data members to the values
4995 The above functions can be invoked with a NULL `soap` context, but you are then
4996 responsible to use `delete T` to remove this instance from the unmanaged heap.
4998 For example, to allocate a managed `std::string` you can use:
5001 std::string *s = soap_new_std__string(soap);
5004 Primitive types and arrays of these are allocated with `soap_malloc`
5005 (`soap_new_T` calls `soap_malloc` for primitive type `T`). All primitive types
5006 (i.e. no classes, structs, class templates, containers, and smart pointers) are
5007 allocated with `soap_malloc` for reasons of efficiency.
5009 You can use a C++ template to simplify the managed allocation and initialization
5010 of primitive values as follows (this is for primitive types only):
5014 T * soap_make(struct soap *soap, T val) throw (std::bad_alloc)
5016 T *p = (T*)soap_malloc(soap, sizeof(T));
5018 throw std::bad_alloc();
5024 For example, assuming we have the following class:
5030 std::string name; // required (non-pointer means minOccurs=1)
5031 uint64_t *SSN; // optional (pointer means minOccurs=0)
5032 ns__record *spouse; // optional (pointer means minOccurs=0)
5036 You can instantiate a record by using the auto-generated
5037 `soap_new_set_ns__record` and use `soap_make` to create a SSN value on the
5038 managed heap as follows:
5041 soap *soap = soap_new(); // new context
5043 ns__record *record = soap_new_set_ns__record(
5046 soap_make<uint64_t>(soap, 1234567890UL),
5049 soap_destroy(soap); // delete record and all other managed instances
5050 soap_end(soap); // delete managed soap_malloc'ed heap data
5051 soap_free(soap); // delete context
5054 All data on the managed heap is mass-deleted with `soap_end(soap)` which must
5055 be called before `soap_done(soap)` or `soap_free(soap)`, which end the use of
5056 the `soap` context and free the context, respectively. Use
5057 `soap_free(soap)` only when the context is allocated with `soap_new()`. Use
5058 `soap_done(soap)` only when the context is stack allocated (so cannot be
5059 deleted from the heap).
5061 The managed heap is checked for memory leaks at run time when the source code
5062 is compiled with option <b>`-DDEBUG`</b>.
5064 However, the serializer can serialize any heap, stack, or static allocated
5065 data. So we can also create a new record as follows:
5068 uint64_t SSN = 1234567890UL;
5069 ns__record *record = soap_new_set_ns__record(soap, "Joe", &SSN, NULL);
5072 which will be fine to serialize this record as long as the local `SSN`
5073 stack-allocated value remains in scope when invoking the serializer and/or
5074 using `record`. It does not matter if `soap_destroy` and `soap_end` are called
5075 beyond the scope of `SSN`.
5077 To facilitate class methods to access the managing context, we can add a soap
5078 context pointer to a class/struct:
5084 void create_more(); // needs a context to create more internal data
5086 struct soap *soap; // the context that manages this instance, or NULL
5090 The `soap` context pointer member of the class is set when invoking
5091 `soap_new_T` (and similar) with a non-NULL context argument that will be
5092 assigned to the `soap` member of the class.
5094 You can also use a template when an array of pointers to values is required.
5095 To create an array of pointers to values, define the following template:
5099 T **soap_make_array(struct soap *soap, T* array, int n) throw (std::bad_alloc)
5101 T **p = (T**)soap_malloc(soap, n * sizeof(T*));
5103 throw std::bad_alloc();
5104 for (int i = 0; i < n; ++i)
5110 The `array` parameter is a pointer to an array of `n` values. The template
5111 returns an array of `n` pointers that point to the values in that array:
5114 // create an array of 100 pointers to 100 records
5116 ns__record **precords = soap_make_array(soap, soap_new_ns__record(soap, n), n);
5117 for (int i = 0; i < n; ++i)
5119 precords[i]->name = "...";
5120 precords[i]->SSN = soap_make<uint64_t>(1234567890UL + i);
5124 Note that `soap_new_ns__record(soap, n)` returns a pointer to an array of `n`
5125 records, which is then used to create an array of `n` pointers to these records.
5127 Use the soapcpp2 auto-generated `soap_dup_T` functions to duplicate data into
5128 another `soap` context (this requires <b>`soapcpp2 -Ec`</b> option <b>`-Ec`</b> to generate), here shown
5129 for C++ with the second argument `dst` NULL to allocate a new managed object:
5132 soap *other_soap = soap_new(); // another context
5133 ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
5135 soap_destroy(other_soap); // delete record and other managed instances
5136 soap_end(other_soap); // delete other data (the SSNs on the heap)
5137 soap_free(other_soap); // delete context
5140 To duplicate base and derived instances when a base class pointer or reference
5141 is provided, use the auto-generated method `T * T::soap_dup(struct soap*)`:
5144 soap *other_soap = soap_new(); // another context
5145 ns__record *other_record = record->soap_dup(other_soap);
5147 soap_destroy(other_soap); // delete record and other managed instances
5148 soap_end(other_soap); // delete other data (the SSNs on the heap)
5149 soap_free(other_soap); // delete context
5152 The only reason to use another context and not to use the primary `soap`
5153 context is when the primary context must be destroyed together with all of the
5154 objects it manages while some of the objects must be kept alive. If the
5155 objects that are kept alive contain deep cycles then this is the only option we
5156 have, because deep copy with a managing `soap` context detects and preserves
5157 these cycles unless the `SOAP_XML_TREE` flag is used with the context:
5160 soap *other_soap = soap_new1(SOAP_XML_TREE); // another context
5161 ns__record *other_record = record->soap_dup(other_soap); // deep tree copy
5164 The resulting deep copy will be a full copy of the source data structure as a
5165 tree without co-referenced data (i.e. no digraph) and without cycles. Cycles
5166 are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
5168 You can also deep copy into unmanaged space and use the auto-generated
5169 `soap_del_T()` function or the `T::soap_del()` method (requires
5170 <b>`soapcpp2 -Ec`</b> option <b>`-Ec`</b> to generate) to delete it later,
5171 but we should not do this for any data that has deep cycles in its runtime data
5175 ns__record *other_record = record->soap_dup(NULL);
5177 other_record->soap_del(); // deep delete record data members
5178 delete other_record; // delete the record
5181 Cycles in the data structure will lead to non-termination when making unmanaged
5182 deep copies. Consider for example:
5187 const char *name 1; // required (minOccurs=1)
5188 uint64_t SSN; // required (non-pointer means minOccurs=1)
5189 ns__record *spouse; // optional (pointer means minOccurs=1)
5193 The code to populate a structure with a mutual spouse relationship:
5196 soap *soap = soap_new();
5198 ns__record pers1, pers2;
5200 pers1.SSN = 1234567890;
5201 pers1.spouse = &pers2;
5202 pers2.name = "Jane";
5203 pers2.SSN = 1987654320;
5204 pers2.spouse = &pers1;
5206 ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
5207 ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5208 soap_set_mode(soap, SOAP_XML_TREE);
5209 ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5212 The serializer can serialize any heap, stack, or static allocated data, such as
5213 shown in the code shown above. So we can serialize the stack-allocated `pers1`
5217 FILE *fp = fopen("record.xml", "w");
5220 soap->sendfd = fileno(fp); // file descriptor to write to
5221 soap_set_mode(soap, SOAP_XML_GRAPH); // support id-ref w/o requiring SOAP
5222 soap_clr_mode(soap, SOAP_XML_TREE); // if set, clear
5223 if (soap_write_ns__record(soap, &pers1))
5224 ... // handle IO error
5226 soap->sendfd = -1; // block further writing
5230 which produces an XML document record.xml that is similar to:
5234 <ns:record xmlns:ns="urn:types" id="Joe">
5236 <SSN>1234567890</SSN>
5239 <SSN>1987654320</SSN>
5240 <spouse ref="#Joe"/>
5246 Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph
5247 leads to the same non-termination problem when we later try to copy the data
5248 into unmanaged space:
5251 soap *soap = soap_new1(SOAP_XML_GRAPH); // support id-ref w/o SOAP
5254 FILE *fp = fopen("record.xml", "r");
5257 soap->recvfd = fileno(fp); // file descriptor to read from
5258 if (soap_read_ns__record(soap, &pers1))
5259 ... // handle IO error
5261 soap->recvfd = -1; // block further reading
5264 ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
5265 ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5266 soap_set_mode(soap, SOAP_XML_TREE);
5267 ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5270 Copying data with `soap_dup_T(soap)` into managed space is always safe. Copying
5271 into unmanaged space requires diligence. But deleting unmanaged data is easy
5272 with `soap_del_T()`.
5274 You can also use `soap_del_T()` to delete structures in C++, but only if these
5275 structures are created with `new` (and `new []` for arrays when applicable) for
5276 classes, structs, and class templates and with `malloc` for anything else, and
5277 the structures do NOT contain pointers to stack and static data.
5279 You can unlink one or more allocated objects from the managed heap to allow the
5280 object to live after `soap_destroy(soap)` and `soap_end(soap)` by using:
5282 - `void soap_unlink(struct soap *soap, void *ptr)`
5284 The unlinked heap-allocated data pointed to by `ptr` can be accessed after
5285 `soap_destroy(soap)` and `soap_end(soap)`. Do not forget to free the data with
5286 `delete ptr` (C++ class instance only) or with `free(ptr)` (non-class data).
5287 Be aware that `soap_unlink(soap, ptr)` does not perform a deep unlinkage. If
5288 `ptr` is a struct or class, pointer members will become invalid when pointing
5289 to objects on the managed heap. Use `soap_unlink(soap, ptr->member)` to unlink
5292 Finally, when data is allocated in managed memory heap space, either explicitly
5293 with the allocation functions shown above or by the soapcpp2-generated
5294 deserializers, you can delegate the management and deletion of this data to
5295 another `soap` context. That context will be responsible to delete the data
5296 with `soap_destroy(soap)` and `soap_end(soap)` later:
5298 - `void delegate_deletion(struct soap *soap_from, struct soap *soap_to)`
5300 This allows the `soap_from` context to be deleted with `soap_free(soap_from)`
5301 (assuming it is allocated with `soap_new()`, use `soap_done(soap_from)` when
5302 `soap_from` is stack-allocated) while the managed data remains intact. You
5303 can use this function any time, to delegate management and deletion to another
5304 context `soap_to` and then continue with the current context. You can also use
5305 different source `soap_from` contexts to delegate management and deletion to
5306 the other `soap_to` context. To mass delete all managed data, use
5307 `soap_destroy(soap_to)` followed by `soap_end(soap_to)`.
5309 🔝 [Back to table of contents](#)
5311 Context flags to initialize the soap struct {#flags}
5312 ===========================================
5314 There are several `soap` context initialization flags and context mode flags to
5315 control XML serialization at runtime. The flags are set with `soap_new1()` to
5316 allocate and initialize a new context:
5319 struct soap *soap = soap_new1(flag1 | flag2 | ... | flagn);
5321 soap_destroy(soap); // delete objects
5322 soap_end(soap); // delete other data and temp data
5323 soap_free(soap); // free context
5326 and with `soap_init1()` for stack-allocated contexts:
5330 soap_init1(&soap, flag1 | flag2 | ... | flagn);
5332 soap_destroy(&soap); // delete objects
5333 soap_end(&soap); // delete other data and temp data
5334 soap_done(&soap); // clear context
5337 where `flag1`, `flag2`, ..., `flagn` is one of:
5339 - `SOAP_C_UTFSTRING`: enables all `std::string` and `char*` strings to
5340 contain UTF-8 content. This option is recommended.
5342 - `SOAP_C_NILSTRING`: treat empty strings as if they were NULL pointers, i.e.
5343 omits elements and attributes when empty.
5345 - `SOAP_XML_STRICT`: strictly validates XML while deserializing. Should not be
5346 used together with SOAP 1.1/1.2 encoding style of messaging. Use
5347 <b>`soapcpp2 -s`</b> option <b>`-s`</b> to hard code `SOAP_XML_STRICT` in the
5348 generated serializers. Not recommended with SOAP 1.1/1.2 encoding style
5351 - `SOAP_XML_INDENT`: produces indented XML.
5353 - `SOAP_XML_CANONICAL`: c14n canonocalization, removes unused `xmlns` bindings
5354 and adds them to appropriate places by applying c14n normalization rules.
5355 Should not be used together with SOAP 1.1/1.2 encoding style messaging.
5357 - `SOAP_XML_TREE`: write tree XML without id-ref, while pruning data structure
5358 cycles to prevent nontermination of the serializer for cyclic structures.
5360 - `SOAP_XML_GRAPH`: write graph (digraph and cyclic graphs with shared pointers
5361 to objects) using id-ref attributes. That is, XML with SOAP multi-ref
5362 encoded id-ref elements. This is a structure-preserving serialization format,
5363 because co-referenced data and also cyclic relations are accurately represented.
5365 - `SOAP_XML_DEFAULTNS`: uses xmlns default namespace declarations, assuming
5366 that the schema attribute form is "qualified" by default (be warned if it is
5367 not, since attributes in the null namespace will get bound to namespaces!).
5369 - `SOAP_XML_NIL`: emit empty element with <i>`xsi:nil`</i> for all NULL pointers
5372 - `SOAP_XML_IGNORENS`: the XML parser ignores XML namespaces, i.e. element and
5373 attribute tag names match independent of their namespace.
5375 - `SOAP_XML_NOTYPE`: removes all <i>`xsi:type`</i> attribuation. This option is usually
5376 not needed unless the receiver rejects all <i>`xsi:type`</i> attributes. This option
5377 may affect the quality of the deserializer, which relies on <i>`xsi:type`</i>
5378 attributes to distinguish base class instances from derived class instances
5379 transported in the XML payloads.
5381 - `SOAP_IO_CHUNK`: to enable HTTP chunked transfers.
5383 - `SOAP_IO_STORE`: full buffering of outbound messages.
5385 - `SOAP_ENC_ZLIB`: compress messages, requires compiling with option <b>`-DWITH_GZIP`</b> and
5386 linking with zlib using option <b>`-lz`</b>.
5388 - `SOAP_ENC_MIME`: enable MIME attachments, see
5389 [DIME/MIME/MTOM attachment binary types](#toxsd10-3).
5391 - `SOAP_ENC_MTOM`: enable MTOM attachments, see
5392 [DIME/MIME/MTOM attachment binary types](#toxsd10-3).
5394 @note C++ Web service proxy and service classes have their own `soap` context, either
5395 as a base class (with <b>`soapcpp2 -i`</b> option <b>`-i`</b>) or as a pointer member `soap` that points to
5396 a context (with <b>`soapcpp2 -j`</b> option <b>`-j`</b>). These contexts are allocated when the proxy or
5397 service is instantiated with context flags that are passed to the constructor.
5399 🔝 [Back to table of contents](#)
5401 Context parameter settings {#params}
5402 ==========================
5404 After allocation and initializtion of a `soap` context, several context
5405 parameters can be set (some parameters may require 2.8.31 or greater):
5407 - `unsigned int soap::maxlevel` is the maximum XML nesting depth levels that
5408 the parser permits. Default initialized to `SOAP_MAXLEVEL` (10000), which is
5409 a redefinable macro in <i>`gsoap/stdsoap2.h`</i>. Set `soap::maxlevel` to a
5410 lower value to restrict XML parsing nesting depth.
5412 - `long soap::maxlength` is the maximum string content length if not already
5413 constrained by an XML schema validation `maxLength` constraint. Zero means
5414 unlimited string lengths are permitted (unless restricted by XML schema
5415 `maxLength`). Default initialized to `SOAP_MAXLENGTH` (0), which is a
5416 redefinable macro in <i>`gsoap/stdsoap2.h`</i>. Set `soap::maxlength` to a
5417 positive value to restrict the number of (wide) characters in strings parsed,
5418 restrict hexBinary byte length, and restrict base64Binary byte length.
5420 - `size_t soap::maxoccurs` is the maximum number of array or container elements
5421 permitted by the parser. Must be greater than zero (0). Default initialized
5422 to `SOAP_MAXOCCURS` (100000), which is a redefinable macro in
5423 <i>`gsoap/stdsoap2.h`</i>. Set `soap::maxoccurs` to a positive value to
5424 restrict the number of array and container elements that can be parsed.
5426 - `soap::version` is the SOAP version used, with 0 for non-SOAP, 1 for SOAP1.1,
5427 and 2 for SOAP1.2. This value is normally set by web service operations, and
5428 is otherwise 0 (non-SOAP). Use `soap_set_version(struct soap*, short)` to
5429 set the value. This controls XML namespaces and SOAP id-ref serialization
5430 when applicable with an encodingStyle (see below).
5432 - `const char *soap::encodingStyle` is a string that is used with SOAP
5433 encoding, normally NULL for non-SOAP XML. Set this string to "" (empty
5434 string) to enable SOAP encoding style, which supports id-ref graph
5435 serialization (see also the `SOAP_XML_GRAPH` [context flag](#flags)).
5437 - `int soap::recvfd` is the file descriptor to read and parse source data from.
5438 Default initialized to 0 (stdin). See also [input and output](#io).
5440 - `int soap::sendfd` is the file descriptor to write data to. Default
5441 initialized to 1 (stdout). See also [input and output](#io).
5443 - `const char *is` for C: string to read and parse source data from, overriding
5444 the `recvfd` source. Normally NULL. This value must be reset to NULL or
5445 the parser will continue to read from this string content until the NUL
5446 character. See also [input and output](#io).
5448 - `std::istream *is` for C++: an input stream to read and parse source data
5449 from, overriding the `recvfd` source. Normally NULL. This value must be
5450 reset to NULL or the parser will continue to read from this stream until EOF.
5451 See also [input and output](#io).
5453 - `const char **os` for C: points to a string (a `const char *`) that will be
5454 set to point to the string output. Normally NULL. This value must be reset
5455 to NULL or the next output will result in reassigning the pointer to point to
5456 the next string that is output. The strings are automatically deallocated by
5457 `soap_end(soap)`. See also [input and output](#io).
5459 - `std::ostream *os` for C++: an output stream to write output to. Normally
5460 NULL. This value must be reste to NULL or the next output will be send to
5461 this stream. See also [input and output](#io).
5463 🔝 [Back to table of contents](#)
5465 Error handling and reporting {#errors}
5466 ============================
5468 The gSOAP API functions return `SOAP_OK` (zero) or a non-zero error code. The
5469 error code is stored in `int soap::error` of the current `soap` context.
5470 Error messages can be displayed with:
5472 - `void soap_stream_fault(struct soap*, std::ostream &os)` for C++ only, prints
5473 the error message to an output stream.
5475 - `void soap_print_fault(struct soap*, FILE *fd)` prints the error message to a
5478 - `void soap_sprint_fault(struct soap*, char *buf, size_t len)` saves the error
5479 message to a fixed-size buffer allocated with a maximum length.
5481 - `void soap_print_fault_location(struct soap*, FILE *fd)` prints the location
5482 and part of the XML where the parser encountered an error.
5484 C++ exceptions are never raised by the engine or serializers, even when data is
5485 allocated. (That is unless the `SOAP_NOTHROW` macro (set to `(std::nothrow)`
5486 by default) is redefined to permit `new` to throw exceptions.)
5488 A `SOAP_EOM` error code is returned when memory was exhausted during
5489 processing of input and/or output of data.
5491 An EOF (`SOAP_EOF` or -1) error code is returned when the parser has hit EOF
5492 but expected more input, or when socket communications timed out. In addition
5493 to the `SOAP_EOF` error, the `int soap::errnum` of the `soap` context is
5494 set to the `errno` value of the operation that failed. For timeouts, the
5495 `soap::ernum` value is always 0 instead of an `errno` error code.
5497 Use `soap_xml_error_check(soap->error)` to check for XML errors. This returns
5498 true (non-zero) when a parsing and validation error has occurred.
5505 struct soap *soap = soap_new1(SOAP_XML_INDENT | SOAP_XML_STRICT | SOAP_XML_TREE);
5506 struct ns__record person;
5507 std::stringstream ss;
5508 ss.str("..."); // XML to parse
5510 if (soap_read__ns__record(soap, &person))
5512 if (soap_xml_error_check(soap->error))
5513 std::cerr << "XML parsing error!" << std::endl;
5515 soap_stream_fault(soap, std::cerr);
5519 ... // all OK, use person record
5521 soap_destroy(soap); // delete objects
5522 soap_end(soap); // delete other data and temp data
5523 soap_free(soap); // free context
5526 When deploying your application on UNIX and Linux systems, UNIX signal handlers
5527 should be added to your code handle signals, in particular `SIGPIPE`:
5530 signal(SIGPIPE, sigpipe_handler);
5533 where the `sigpipe_handler` is a function:
5536 void sigpipe_handler(int x) { }
5539 Other UNIX signals may have to be handled as well.
5541 The engine is designed for easy memory cleanup after being interrupted. Use
5542 `soap_destroy(soap)` and `soap_end(soap)`, after which the `soap` context can
5545 🔝 [Back to table of contents](#)
5547 Features and limitations {#features}
5548 ========================
5550 In general, to use the generated code:
5552 - Make sure to `#include "soapH.h"` in your code and also define a namespace
5553 table or `#include "ns.nsmap"` with the generated table, where `ns` is the
5554 namespace prefix for services.
5556 - Use <b>`soapcpp2 -j`</b> option <b>`-j`</b> (C++ only) to generate C++ proxy and service objects.
5557 The auto-generated files include documented inferfaces. Compile with
5558 <i>`soapC.cpp`</i> and link with <b>`-lgsoap++`</b>, or alternatively compile
5559 <i>`gsoap/stdsoap2.cpp`</i>.
5561 - Without <b>`soapcpp2 -j`</b> option <b>`-j`</b>: client-side uses the auto-generated
5562 <i>`soapClient.cpp`</i> and <i>`soapC.cpp`</i> (or C versions of those).
5563 Compile and link with <b>`-lgsoap++`</b> (<b>`-lgsoap`</b> for C), or
5564 alternatively compile <i>`gsoap/stdsoap2.cpp`</i> (<i>`gsoap/stdsoap2.c`</i>
5567 - Without <b>`soapcpp2 -j`</b> option <b>`-j`</b>: server-side uses the
5568 auto-generated <i>`soapServer.cpp`</i> and <i>`soapC.cpp`</i> (or C versions
5569 of those). Compile and link with <b>`-lgsoap++`</b> (<b>`-lgsoap`</b> for
5570 C), or alternatively compile <i>`gsoap/stdsoap2.cpp`</i> (<i>`stdsoap2.c`</i>
5573 - Use `soap_new()` or `soap_new1(int flags)` to allocate and initialize a
5574 heap-allocated `soap` context with or without flags. Delete this `soap` context with
5575 `soap_free(struct soap*)`, but only after `soap_destroy(struct soap*)` and
5576 `soap_end(struct soap*)`.
5578 - Use `soap_init(struct *soap)` or `soap_init1(struct soap*, int flags)` to
5579 initialize a stack-allocated `soap` context with or without flags. End the use of
5580 this context with `soap_done(struct soap*)`, but only after
5581 `soap_destroy(struct soap*)` and `soap_end(struct soap*)`.
5583 Additional notes with respect to the wsdl2h and soapcpp2 tools:
5585 - Nested classes, structs, and unions in a interface header file are unnested
5588 - Use `#import "file.h"` instead of `#include` to import other header files in
5589 a interface header file for soapcpp2. The `#include`, `#define`, and
5590 `#pragma` are accepted by soapcpp2, but are moved to the very start of the
5591 generated code for the C/C++ compiler to include before all generated
5592 definitions. Often it is useful to add an `#include` with a
5593 [volatile type](#toxsd9-2) that includes the actual type declaration, and to
5594 ensure transient types are declared when these are used in a data binding
5595 interface declared in a interface header file for soapcpp2.
5597 - To remove any SOAP-specific bindings, use <b>`soapcpp2 -0`</b> option <b>`-0`</b>.
5599 - A interface header file for soapcpp2 should not include any code statements,
5600 only data type declarations. This includes constructor initialization lists
5601 that are not permitted. Use member initializations instead.
5603 - C++ namespaces are supported. Use <b>`wsdl2h -qname`</b> option
5604 <b>`-qname`</b> to add C++ namespace `name`. Or add a `namespace name { ... }`
5605 to the header file, but the `{ ... }` must cover the entire
5606 header file content from begin to end.
5608 - Optional XML DOM support can be used to store mixed content or literal XML
5609 content. Otherwise, mixed content may be lost. Use <b>`wsdl2h -d`</b>
5610 option <b>`-d`</b> for XML DOM support and compile and link with
5611 <i>`gsoap/dom.c`</i> or <i>`gsoap/dom.cpp`</i>. For details,
5612 see [XML DOM and XPath](http://www.genivia.com/doc/dom/html).
5614 🔝 [Back to table of contents](#)
5616 Removing SOAP namespaces from XML payloads {#nsmap}
5617 ==========================================
5619 The soapcpp2 tool generates a <i>`.nsmap`</i> file that includes two bindings for SOAP
5620 namespaces. We can remove all SOAP namespaces (and SOAP processing logic) with
5621 <b>`soapcpp2 -0`</b> option <b>`-0`</b> or by simply setting the two entries to NULL:
5624 struct Namespace namespaces[] =
5626 {"SOAP-ENV", NULL, NULL, NULL},
5627 {"SOAP-ENC", NULL, NULL, NULL},
5632 Once the <i>`.nsmap`</i> is generated, you can copy-paste the content into your
5633 project code. However, if we rerun wsdl2h on updated WSDL/XSD files or
5634 <i>`typemap.dat`</i> declarations then we need to use the updated table.
5636 In cases that no XML namespaces are used at all, for example with
5637 [XML-RPC](http://www.genivia.com/doc/xml-rpc-json/html), you may use an empty
5641 struct Namespace namespaces[] = {{NULL,NULL,NULL,NULL}};
5644 However, beware that any built-in xsi attributes that are rendered will lack
5645 the proper namespace binding. At least we suggest to use `SOAP_XML_NOTYPE` for
5648 🔝 [Back to table of contents](#)
5650 Examples {#examples}
5653 Select the project files below to peruse the source code examples.
5655 🔝 [Back to table of contents](#)
5660 - <i>`address.xsd`</i> Address book schema
5661 - <i>`address.cpp`</i> Address book app (reads/writes address.xml file)
5662 - <i>`addresstypemap.dat`</i> Schema namespace prefix name preference for wsdl2h
5663 - <i>`graph.h`</i> Graph data binding (tree, digraph, cyclic graph)
5664 - <i>`graph.cpp`</i> Test graph serialization as tree, digraph, and cyclic
5666 🔝 [Back to table of contents](#)
5671 - <i>`address.h`</i> data binding interface generated from address.xsd
5672 - <i>`addressStub.h`</i> C++ data binding definitions
5673 - <i>`addressH.h`</i> Serializers
5674 - <i>`addressC.cpp`</i> Serializers
5675 - <i>`address.xml`</i> Address book data generated by address app
5676 - <i>`graphStub.h`</i> C++ data binding definitions
5677 - <i>`graphH.h`</i> Serializers
5678 - <i>`graphC.cpp`</i> Serializers
5679 - <i>`g.xsd`</i> XSD schema with <i>`g:Graph`</i> complexType
5680 - <i>`g.nsmap`</i> xmlns bindings namespace mapping table
5682 🔝 [Back to table of contents](#)
5687 Building the AddressBook example:
5689 wsdl2h -g -taddresstypemap.dat address.xsd
5690 soapcpp2 -0 -C -S -paddress -I../../import address.h
5691 c++ -I../.. address.cpp addressC.cpp -o address -lgsoap++
5693 Using <b>`wsdl2h -g -taddresstypemap.dat`</b> option <b>`-g`</b> produces
5694 bindings for global (root) elements in addition to types and option
5695 <b>`-taddresstypemap.dat`</b> specifies a mapping file, see further below.
5697 In this case the root element <i>`a:address-book`</i> is bound to `_a__address_book`.
5698 The complexType <i>`a:address`</i> is bound to class `a__address`, which is also the
5699 type of `_a__address_book`. This option is not required, but allows you to use
5700 global element tag names when referring to their serializers, instead of their
5701 type name. Using <b>`soapcpp2 -0 -C -S -paddress`</b> option <b>`-0`</b> removes the
5702 SOAP protocol and the combination of the two options <b>`-C`</b> and
5703 <b>`-S`</b> removes client and server code generation (using option <b>`-C`</b>
5704 alone generates client code and using option <b>`-S`</b> alone generates server
5705 code). Option <b>`-paddress`</b> renames the output <i>`soap`</i>-prefixed files to
5706 <i>`address`</i>-prefixed files.
5708 See the <i>`address.cpp`</i> implementation and [related pages](pages.html).
5710 The <i>`addresstypemap.dat`</i> file specifies the XML namespace prefix for the
5713 # Bind the address book schema namespace to prefix 'a'
5715 a = "urn:address-book-example"
5717 # By default the xsd:dateTime schema type is translated to time_t
5718 # To map xsd:dateTime to struct tm, enable the following line:
5720 # xsd__dateTime = #import "../../custom/struct_tm.h"
5722 # ... and compile/link with custom/struct_tm.c
5724 The DOB field is a <i>`xsd:dateTime`</i>, which is bound to `time_t` by default. To
5725 change this to `struct tm`, enable the import of the `xsd__dateTime` custom
5726 serializer by uncommenting the definition of `xsd__dateTime` in
5727 <i>`addresstypemap.dat`</i>. Then change `soap_dateTime2s` to `soap_xsd__dateTime2s`
5730 Building the graph serialization example:
5732 soapcpp2 -C -S -pgraph -I../../import graph.h
5733 c++ -I../.. graph.cpp graphC.cpp -o graph -lgsoap++
5735 To compile without using the <b>`-lgsoap++`</b> library: simply compile
5736 <i>`stdsoap2.cpp`</i> together with the above.
5738 🔝 [Back to table of contents](#)
5743 To execute the AddressBook example:
5747 To execute the Graph serialization example: