Need to parse python dictionaries into xml

Ian Kelly ian.g.kelly at gmail.com
Wed Jun 16 13:43:10 EDT 2010


On Wed, Jun 16, 2010 at 10:46 AM, abhijeet thatte
<abhijeet.thatte at gmail.com> wrote:
> I am parsing some hardware format which finally I need to convert in xml.
> The intermediate step is dicts.
> So, the structure looks like this:
> {chip_name:'myChip',chip_clock:'3.07',chip_peripherals:{peripheral1:{mode:'mode1',register:{}},peripheral2:{....},peripheral3:{...}}}
> I think this example gives good insight into kind of dict structure I am
> having. Now I want to have every dict tag as xml tag with hierarchy
> maintained.

You could traverse the dict, using xml.etree.ElementTree.TreeBuilder
to build up an element tree, then use ElementTree.write() to generate
the xml file.  It's not clear why you need the dict, though.  Why not
just use the element tree as your intermediate representation?

Cheers,
Ian



More information about the Python-list mailing list