- xml-cpp-class-generator Wiki
- Build header and implementation file from XML description.
- Home
- Introduction
- Package contents
- XmlCppClassGenerator contents
- List of xml files
- Help on the package
- Requirements
- Liquid XML Data Binder
- What is XML Data Binding?
- Liquid XML Data Binder Features
- XML Code Generator for C++
- Platform Support
- Memory and Object Management
- Ease of Use
- Strongly Typed Code
- Customisable Code
- Tutorials
- Example Code
- Video Tutorials
- XML Data Binding
- A Simple XML Data Binding Example for C++
- In Summary
- Easier to Code
- Easier to Test
- Easier to Maintain
- xml-cpp-class-generator Wiki
- Build header and implementation file from XML description.
- HowTo
- Introduction
- Usage
- More documentation
xml-cpp-class-generator Wiki
Build header and implementation file from XML description.
Home
- Introduction
- Package contents
- XmlCppClassGenerator contents
- List of xml files
Introduction
This package aims at producing nice header and implementation files for C++ classes and structures. Being a C++ developer myself, I like to have my class members to be private or protected, meaning that set/get methods have to be written. I also try to add meaningful comments, which becomes a pain in the neck when class A has around 20 protected members. I tried then to find a way to automatically build the header and implementation files from a generic class description. I didn’t find anything fulfilling my purpose, then I decided to code it myself. Some examples can be found here.
I work a lot with XML files, they have the advantage to be both human and machine readable. This is why the class description is written in a XML file. This file contains information about the class name, its author, the constructors and methods (with their arguments), the members, parent class(es), etc. A complete description can be found in the XML file description page.
Since the package has to deal with string manipulation, my first idea was to use Perl, but the class hierarchy I needed was more compatible with Python. Moreover, the xml.dom.minidom python module makes XML manipulation easy. Therefore the chosen language for this package is Python.
Package contents
The package consists of two parts: the python module XmlCppClassGenerator and a directory (XmlCppClassGeneratorInfo) containing the XML schema describing the structure of the XML class file, a translation XML file with the corresponding XML schema file.
XmlCppClassGenerator contents
The XmlCppClassGenerator module consists of four related submodules :
- a module defining python classes, each of them representing various objects found in a C++ class definition, like method, argument, member, .
- a module defining a customisable output messaging service, allowing to put debug, warning statements in the code, which can be enabled or disabled at runtime.
- a module responsible for providing character strings depending on the wanted language (used for class generation and output in the terminal).
- a module responsible for the XML reading and creation of the representation of the C++ class, this object being used to generate both header and implementation file.
List of xml files
The XmlCppClassGeneratorInfo directory contains:
- the XML schema file XmlCppClassGenerator.xsd describing the structure of the XML file containing the class description.
- the XML schema file Languages.xsd, which describes the structure of the translation file.
- the XML file Translations.xml, which contains the translations (at the moment english and french are implemented).
- the XML schema file Statements.xsd, which describes the structure of the file containing C++ statements (basically method bodies).
- the XML file Bodies.xml, which contains the bodies of the different methods.
Help on the package
How to run the package is explain on the how to page.
Since the class you want to create has to be described in a XML file, the structure of this XML file has to be described. In this page is given a full example of a valid XML file.
The translation file (contents, how to write it, . ) is described in this page. With the given information you will be able to add your favourite language to the package.
Finally, information on the XML file providing the method bodies are given in here.
Requirements
The requirements are checked when the installation is performed, by the dedicated ./configure script.
- libxml2-utils for xmllint (XML validation program) is needed.
- the needed python modules are provided by the standard python package (version 2.7 and later), namely :
- minidom
- codecs
- subprocess
- argparse
Liquid XML Data Binder
This 60 second overview shows you the basic concept of generating source code from an XML Schema using Liquid XML Data Binder.
What is XML Data Binding?
XML Data Binding enables you to load XML Documents into a strongly typed object model within your source code.
Meaning fewer coding errors, reduced development and testing time, increased conformance and coding reliability.
Liquid XML Data Binder Features
- Generates an easy to use class library for C++.
- Generated HTML documentation for your class library API.
- Supports Smart Device platforms Android and iOS.
- Supports W3C XML Schema XSD, XDR and DTD standards.
- Supports JSON serialization.
- Supports Fast Infoset binary XML serialization.
- Support for the most complex XML standards.
- Royalty free distribution of compiled code and runtime.
XML Code Generator for C++
Platform Support
The C++ code is platform independent and has successfully been used on Windows, Linux, Solaris, HP-UX, MacOS, embedded systems and other more obscure platforms.
The full C++ runtime source code is available and is easily compiled onto other platforms (e.g. HP-UX, Mac OS, embedded systems).
Memory and Object Management
Memory Management is performed via reference counting, so no memory leaks. Smart Pointer classes are employed meaning you don’t have to deal with any of the referencing counting. References are automatically released when your smart pointer goes out of scope.
Ease of Use
Strongly Typed Code
Liquid XML Data Binder creates simple intuitive code from your XML Schema. The generated code library contains strongly typed classes, collections, and enumerations to create an intuitive custom API to code against. Allowing you to concentrate on writing your Business logic code.
Collections
Collections are strongly typed and use the stl iterator pattern.
Properties
The generated classes expose the values in the XML as simple properties (getters and setters) allowing values to be easily manipulated.
Customisable Code
The generated output may also be modified within special ‘Hand Coded Blocks’, changes inside these blocks are persisted when the library is re-generated, allowing the library to be customised to fit the projects needs.
Tutorials
Example Code
Video Tutorials
XML Data Binding
This video tutorial shows you how to generate strongly typed API source code from your XSD using the Liquid XML Data Binder.
A Simple XML Data Binding Example for C++
The following C++ example we will use the Person.xsd, shown below to demonstrate how to read and write an XML document based on this schema.
Fred 1978-06-26 7 WV6 6JY Escort Ford Elise Lotus Example C++ code to create the PersonSample.xml document using the strongly typed XML Data Binding generated code:
// create the root item 'person' CPersonPtr spPerson = CPerson::CreateInstance(); spPerson->SetName(_T("Fred")); spPerson->SetDateOfBirth(CDateTime(1978, 6, 26)); // add 'address' CAddressPtr spAddress = CAddress::CreateInstance(); spPerson->SetAddress(spAddress); spPerson->GetAddress()->SetHouseNo(7); spPerson->GetAddress()->SetPostCode(_T("WV6 6JY")); // add 'car1' cars to collection CCarPtr spCar1 = CCar::CreateInstance(); spPerson->GetCars()->Add(spCar1); spCar1->SetMake(_T("Ford")); spCar1->SetModel(_T("Escort")); // add 'car2' cars to collection CCarPtr spCar2 = CCar::CreateInstance(); spPerson->GetCars()->Add(spCar2); spCar2->SetMake(_T("Lotus")); spCar2->SetModel(_T("Elise")); // write xml document to string printf(_T("XML = %s"), spPerson->ToXml().c_str()); // write xml document to file spPerson->ToXmlFile(_T("SampleFile.xml"));
The XML Data Binding code is much easier to read and more concise than equivalent code written using generic DOM technology.
The strongly typed API allows errors to be detected at compile time should the data model change as opposed to DOM code where any errors will only be picked up in testing.
Example C++ code to read the PersonSample.xml document using XML Data Binding:
// create the root item 'person' CPersonPtr spPerson = CPerson::CreateInstance(); // load the items into the model spPerson->FromXmlFile(_T("SampleFile.xml")); // read items from the model printf(_T("%s was born %s"), spPerson->GetName().c_str(), spPerson->GetDateOfBirth().ToString().c_str()); printf(_T(", and lives at %d, %s\n"), spPerson->GetAddress()->GetHouseNo(), spPerson->GetAddress()->GetPostCode().c_str()); printf(_T("Cars Owned (%d)\n"), spPerson->GetCars()->GetCount()); for (CCarCol::iterator itr = spPerson->GetCars()->begin(); itr != spPerson->GetCars()->end(); itr++) < CCarPtr spCar = *itr; printf(_T(" %s, %s\n"), spCar->GetMake().c_str(), spCar->GetModel().c_str()); >
The XML Data Binding code provides the majority of the parsing for you, so manipulating the XML objects is a simple task of dealing with collections of strongly typed objects.
This is much easier than using generic DOM code, where you need to continually check the data type of an item.
In Summary
Easier to Code
XML Data Binding makes it significantly easier to deal with XML documents from within your C++ code, resulting in less code, which is simpler to read and maintain.
Easier to Test
As the generated class library is strongly typed, it forms a template for the developer, ensuring that the data created is valid and conforms to the underlying XML Schema.
Easier to Maintain
The maintenance phase of a project also befits as XML Data Binding allows any errors introduced because of changes in the data model to be caught early at compile time, unlike weakly typed DOM trees, which will give no indication of a problem until runtime. These kinds of changes can be a major cause of bugs which can only be caught in testing. Being able to identify these at compile time can save a significant amount of time and effort.
xml-cpp-class-generator Wiki
Build header and implementation file from XML description.
HowTo
Introduction
The package can be retrieved either from the svn repository:
svn checkout http://xml-cpp-class-generator.googlecode.com/svn/trunk/ MyClassGenerator
or from the downloads page. and should be installed like this:
cd MyClassGenerator ./configure --prefix=/usr make install
NB: the prefix (/usr in the given example) should correspond to where the python modules should be installed. On many distributions it is
/usr/lib[64]/python[version]/site-packages/
whilst the default prefix is usually /usr/local, which will not work.
Once this is done, it can be run by calling GenerateFiles.py from anywhere (be sure you run rehash on your (t)csh or zsh already opened shells).
I found a proper way to configure a local installation:
cd MyClassGenerator ./configure --enable-local make
This allows to run the package like this:
cd MyClassGenerator ./GenerateFiles.py
Usage
From now onwards, it is assumed that MyClassDescription.xml, a valid XML class description file (as described here), is present in the directory.
First, help is provided by the executable script itself, when using the -h or —help argument:
usage: GenerateFiles.py [-h] [-v] [-H HEADEREXT] [-I IMPLEXT] [-d OUTPUTDIR] [-l LANG] [-n] [-1 LEVEL1] [-2 LEVEL2] F [F . ]
Generates C++ classes from a class description read from a XML file.
positional arguments: F XML file(s) to be read
optional arguments: -h, —help show this help message and exit -v, —version show program’s version number and exit -H HEADEREXT, —header HEADEREXT extension for header files (default: .hpp) -I IMPLEXT, —impl IMPLEXT extension for implementation files (default: .cpp) -d OUTPUTDIR, —directory OUTPUTDIR destination directory (default: ./) -l LANG, —language LANG sets the output language (default: en) -t, —translation shows available translations and exits -n, —noout disables the file production, will just check syntax (default: False) -1 LEVEL1, —level1 LEVEL1 sets output level for the interpretor class (default: 3) -2 LEVEL2, —level2 LEVEL2 sets output level for the main script (default: 3)
This help and the argument processing are done by the argparse python module.
In order to get the output files in my_output_dir , the following command line has to be typed:
cd MyClassGenerator ./GenerateFiles.py MyClassDescription.xml -d my_output_dir
The -1 and -2 arguments control the verbosity of the running, -l sets the output language (the ISO-639-1 language code), defaults to en (i.e. english) if nothing is given or if the wanted translation does not exist. A list of available translations is printed when using -t argument.
More documentation
All the python modules are documented, the pdf manual can be created using doxygen on the provided doxyfile in Documentation/ . The documentation can also be generated by using make documentation .