[Tutor] parsing XML into a python dictionary

Kent Johnson kent37 at tds.net
Sat Nov 14 14:03:52 CET 2009


On Sat, Nov 14, 2009 at 1:14 AM, Christopher Spears
<cspears2002 at yahoo.com> wrote:
> I've been working on a way to parse an XML document and convert it into a python dictionary.  I want to maintain the hierarchy of the XML.  Here is the sample XML I have been working on:
>
> <collection>
>  <comic title="Sandman" number='62'>
>    <writer>Neil Gaiman</writer>
>    <penciller pages='1-9,18-24'>Glyn Dillon</penciller>
>    <penciller pages="10-17">Charles Vess</penciller>
>  </comic>
> </collection>

> This is the output:
> 163>./parseXML.py
> {'collection': {('comic', {'number': '62', 'title': 'Sandman'}): [<Element writer at -482193f4>, <Element penciller at -482193cc>, <Element penciller at -482193a4>]}}

This seems an odd format. How are you going to use it? How is this
better than the native ElementTree structure?

> The script doesn't descend all of the way down because I'm not sure how to hand a XML document that may have multiple layers.  Advice anyone?  Would this be a job for recursion?

Yes. Here is an example that might be helpful:
http://code.activestate.com/recipes/410469/

Kent


More information about the Tutor mailing list