XML Data Binding for C++

2006-08-06

Strigi has reached the point that the configuration files for it should be more advanced than a text file with one directory per line. Because I have good experience with using XML Schema for mapping from XML to java and back using JAXB, I’d been looking for a good toolkit that does the same in C++. The requirements for such a tool are:

A tool I’ve found is called xsd. Behind this trivially simple name is a suite of free software tools that generate different types of C++ code. The latest version of this tool is 2.2.0 and can be gotten here. There’s an extensive manual starting at a simple Hello World application.

To test the feasibility of this code for Strigi I designed a small XML Schema file and wrote a simple program to accompany it. For Strigi, I’d like to have a configuration file that describes what directories and other sources are indexed and how. Below I’ve written the different file I used for testing xsd. The workflow is pretty easy:

  1. write XML Schema

  2. compile c++ from schema

  3. use data classes in c++ code

If the requirements for the configuration file change, the XML Schema is changed and the c++ code regenerated. Any changes required in the c++ code that uses the data classes will be caught by the compiler.
Nothing is for free: using xsd introduces dependencies in your code. When using xsd, you will need to link your executable with xerces-c. Since this is a widespread library, this is not a big problem.
An example configuration file might look like this:

Post a comment