* * * * * * * * * * * * * * * * * * * * * * * *
http://www.xmlbooster.com --- March 2006
* * * * * * * * * * * * * * * * * * * * * * * *
Generating an XML parser to serialize an existing C data structure
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
XMLBooster allows quite some flexibility when it comes to support complex
XML constructs, and assumes that the application developer has some freedom
about the way these XML documents are represented in the
host language.
However, sometimes, the situation is reversed: one has a - complex -
data structure to serialize in XML form, and the way it is translated onto
XML is of less importance than the fact that the original data structure
must be used. The degree of freedom has thus shifted, from the host
data structure to the corresponding XML representation.
XMLBooster addresses such cases, by using the freely available
RainCode Engine for C (http://www.raincode.com). This product can
read C programs from which it builds data structures representing
its various artefacts, and have scripts perform user-defined
actions by accessing these data structures.
This is exactly what the "struct2meta.rcs" script (included in the
XMLBooster distribution) does: it identifies one or more structure
declarations, and generates all the necessary code to serialize them
to XML, and to parse them back from XML into the data structure.
In addition to the script itself, the "scripts/struct2meta" directory in
the XMLBooster distribution contains a complete example (including a makefile)
demonstrating how to use this script.
The process goes as follows:
- The RainCode Engine reads a C compilation unit (expanding any
#include'd file, if present)
- The "struct2meta.rcs" script is started. It identifies one or more
structures for which an XML parsing and serialization infrastructure
is needed, by means of a regular expression the structure names must
match
- It then generates an XMLBooster meta-definition that where the various
items defined or referred to in these C structures are represented
(field, types, arrays, enumeration)
- It generates additional code to convert from the existing user-defined
data structure to the XMLBooster-generated one, back and forth
- It generates additional wrappers to hide this layered machinery
from the casual user who can just parse and serialize instances
of his/her user-defined data typed using generated C functions.
This is not a one-size-fits-all situation though, as there are many different
ways a C structure can be mapped canonically to an XML representation.
Issues that might have to be taken into account include things such as:
- Using attributes or elements (typically, numeric types can go to
attributes, and textual data can be better be represented by elements)
- Whether parts should be made optional or not.
- How to deal with repetitions, linked lists, etc...
- How to deal with dynamic allocations, for repetitions or character
strings
- Permutations can be used instead of concatenation to allow the various
sub-components to occur in any sequence
- ...
In order to accomodate the issues listed above, the script must be updated
accordingly. Its source code is comprehensive, well-commented, and can be
changed easily.
That's all for today, folks !!!
* * * * * * * * * * * * * * * * * * * * * * * *
XMLBooster Lite is freely available, and can be used to test
XMLBooster extensively.
* * * * * * * * * * * * * * * * * * * * * * * *
Home page: http://www.xmlbooster.com
Log of changes: http://www.xmlbooster.com/log.html
Price list: http://www.xmlbooster.com/pricingandorder.html
Benchmarks: http://www.xmlbooster.com/benchmark.html
* * * * * * * * * * * * * * * * * * * * * * * *