Forum Chat

Mar23,13:31 Johan Marechal
Wees gegroet
Sep20,17:50 Vicente Duque
Kim, Martin, Others :...
Jul07,11:10 Johan Marechal
Jul05,21:13 martin
Fastest in the bush
Jul05,07:48 martin
Jun28,21:16 martin
New domain / new blog!
Jun28,21:11 martin
On posting etiquette

x2c - the xml-to-code generator project

Comment on this article



December 2008 - uploaded the full source under GPL

September 2008 - I'm in the process of not only updating this thing, but also giving it away for free. There's no "trial" or "full" version anymore, just the working version. When I get time, I'll also post the full source, probably with some kind of GPL attached. In the meanwhile, you can download the exe and manual.

November 2001 news: moved this project from being my main thing to not being my main thing. Reason is that I've got too much else to do on the one hand, and that a command-line utility isn't sexy enough nowadays to go anywhere for real. No matter how useful it may have been. I'm thinking of using some boring days up ahead (if they appear) to clean up the source and post it here for general consumption. If I get the time, that is. Meanwhile, the executable is still right here.

What this thing does; it takes one or any number of xml files describing objects and/or databases and generates c++ code for the entire data object layer from it. Or VB code. Or Java code, or whatever you may fancy, since all code generation is defined in template files you yourself write and edit.

Assume, for instance, that you've got a bunch of xml files in a directory, and each of those files should result in a c++ header file and a c++ source file. Then you may accomplish this by writing a template file looking something like this:

// template file 
set filenames = dir("c:\projects\cactus\x2c\inputs\*.xml");
foreach filename in filenames
	node xf = xmlopen(filename);	// gets you the root node 
	// now get the contents of attribute "name" in the root element
	string packname = evalattr(xf, "name");
	// open an output file with that name in the current directory and with the 
	// extension ".h"
	string sOutFileName = filepath(getcurdir()) & "\" & packname & ".xml"; 
	open create sOutFileName;	
	// anything between [[ and ]] is written to the output
	// while expressions between <$ and > are evaluated and replaced on the fly
		// <$>.h
		#ifndef <$packname>_H
		#define <$packname>_H

This may result in a file looking like:

// patient.h
#ifndef patient_H
#define patient_H

The xml definition used, may have looked like:

<? xml version='1.0'?>
<package name='patient'>
  <object name='patperson'>

The '[[' and ']]' thingies deliminate text that is copied out to the output text file. Anything between '<$' and '>' in that output gets replaced by something either in the script itself or looked up from an xml file. In this case, the name attribute of the top level element 'package' is used in place of the entire expression '<$packname>'.

The template language contains syntactical constructs like:

  • foreach - to loop through filenames in directories, nodes in nodelists, etc

  • while - looping while true

  • if, else - conditional

  • open - opening for read or write of both xml and text files; writes textfiles in unicode or MCBS with chosen codepage as a parameter

  • #include - allows logical split of template script files

  • complete numerical, string and logical expression parser

  • map - lookup and replace of numerical and string values

  • set - contains any number of strings or filenames

  • xmlnew, xmlsaveas, addnode, delnode, setattr, delattr.... allowing building and modifying xml documents

  • applying xslt transforms

  • getnode(), getnodes() allowing selection of nodes and nodelists using xpath

  • miscellaneous string functions

Now, get this. I have a pretty good idea what to use this for, namely generating data access layer code using xml descriptions of databases and business layer objects. But I'm sure there are other uses as well. If you have ideas of what to include, drop me a line. The feature set may be very dependent on what input I can get.

Comment on this article