Am 09.10.2012 18:00, schrieb Jean-Michel Pichavant:
I'm trying to generate C++ code from an XML file. I'd like to use a
template engine, which imo produce something readable and
maintainable.
[...]
Here's my flow:

XML file -> nice python app -> C++ code

There is one question that you should answer (or maybe decide?) first: How close is the XML structure to C++ semantically?

The syntactic level is obviously very different, as one uses XML as metaformat while the other is C++. The semantic level is rather about the question if there is e.g. a "<class name='foo'>" that directly translates to a "class foo {" in C++. If that is the case, the SAX API should help you, as it basically invokes callbacks for every XML element encountered while parsing the input stream. In those callbacks, you could then generate the according C++ code in a way that should be readable and maintainable with plain Python or some template engine.

You you need to skip back-and-forth over the input, reading the whole XML as DOM tree would probably be a better approach. Still, the processing of input is separate from output generation, so you could at least divide your task before conquering it.

Notes:
- There is also XSLT which can generate pretty much anything from XML, but it is can't do much more than text replacements triggered by input matching. The more the output differs semantically from the input, the more difficult it becomes to use. Also, XSLT tends to become write-only code, i.e. unreadable. - I think there was a feature in GCC that allows generating XML from C++ input, maybe even the reverse. Maybe you could leverage that?


Good luck!

Uli
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to