[Tutor] parsing XML into a python dictionary
Christopher Spears
cspears2002 at yahoo.com
Sat Nov 14 19:47:17 CET 2009
Thanks! I have a lot of XML files at work that users search through. I want to parse the XML into a python dictionary and then read the dictionary into a database that users can use to search through the thousands of files.
Basically, the user would submit a query like "Neil Gaiman" and then the program would return the name of the files in which the words "Neil Gaiman" appears.
I thought I might be able to use the tags to speed up the search. For example, maybe the program will only look at the "writer" tags, or I can ask the program to show me everything under the "comic" tag.
--- On Sat, 11/14/09, Kent Johnson <kent37 at tds.net> wrote:
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] parsing XML into a python dictionary
> To: "Christopher Spears" <cspears2002 at yahoo.com>
> Cc: tutor at python.org
> Date: Saturday, November 14, 2009, 5:03 AM
> 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