I’ve been asked to look in to the possibility of converting XML to Python, so that the XML can be phased out of the current process & they’d be replaced with new python scripts.
Currently the XML files are used by python & ASP and look essentially like this;
<?xml version="1.0" encoding="UTF-8"?>
<script>
<stage id="stage1" nextStage="stage2">
<initialise>
<variable id="year" value="2012" type="Integer"/>
<executeMethod class="engineclass_date" method="getTodayAsString">
<arguments>
<variable id="someVar"/>
</arguments>
<return>
<variable id="todaysDate"/>
</return>
</executeMethod>
Thanks to Pepr I’ve not got a parser/compiler in the works that produces Python code from the XML. It still needs a lot of work to handle all the possible elements, but it works, and hopefully others can learn from this like I am!
Still need to find a way to write a separate file for each stage of the script in order for the indent to function properly and handle errors properly.
Use the standard xml.etree.Element tree to extract the information from XML to Python objects (or the more enhanced third party lxml with the same API).
I recommend to read the Mark Pilrim’s Dive Into Python 3, Chapter 12. XML (http://getpython3.com/diveintopython3/xml.html).
Here is the core of how the parser/compiler could be written. The idea is to go recursively through the elements, collect the neccessary information and output the code when it is possible:
When used with the data pasted here http://pastebin.com/vRRxfWiA, the script produces the following output: