converting XML to hash/dict/CustomTreeCtrl

Astan Chee astan.chee at al.com.au
Wed Feb 3 14:12:22 EST 2010


Nobody wrote:
> The code you're using expects the XML to follow a particular format, and
> yours doesn't.
>
> You might want to start with a fairly direct translation, e.g.:
>
> def xmldict(e):
>     d = {}
>     d['tag'] = e.tag
>     d.update(e.attrib)
>     children = map(xmldict, e)
>     if children:
> 	d['children'] = children
>     text = e.text.strip()
>     if text:
> 	d['text'] = text
>     return d
>
> tree = ElementTree.parse('test.xml')
> root = tree.getroot()
> d = xmldict(root)
> pprint.pprint(d)
>
> then refine this as needed.
>   
Thanks, that is simple enough for me to understand. I think I got it now.
Thanks again



More information about the Python-list mailing list