XML to dictionary?

Matt Gushee mgushee at havenrock.com
Thu Jan 6 08:03:10 EST 2000


johnjensen at my-deja.com writes:

> I have a relatively simple XML question, but I can't seem to figure out
> how to do it simply and easily with the xmllib module.

If you are only doing the simplest of XML tasks and don't care about
the longevity of the code, xmllib may work for you. But I gather that
it is considered very inadequate for any kind of serious XML
processing. You should look into the XML-SIG's XML package. And you might
want to join the XML-SIG mailing list, too. Look under SIGs --> XML at 
www.python.org.

> How can I quickly and easily stuff this into a nested dictionary, so
> that:
> 
> print dict['Newsgroup']['comp.lang.python']['Message1']['Subject']
> spits out 'Hi there'

What do you consider quick and easy? :)

AFAIK there is no ready-made function to do this for you. But if you
get the data parsed properly, the rest should be easy. You might
consider using Pyxie, which provides a higher-level interface to some
of the XML (SIG) package's functionality. You could do something like:

from pyxie import File2PYX    # PYX is the notation pyxie uses for
import string		      # parsed XML

f = File2PYX('hithere.xml')
data = f.read()
f.close()
data = string.split(data)

dict = {}
for line in data:
    if line[0] = '(':         # '(' at the beginning of the line in 
			      # PYX indicates an element

... hmm. This might be a problem if you don't know exactly how many
levels deep the elements will be nested. But nothing you can't solve
in an hour or so, I'm sure.

By the way, wouldn't you rather have:

<Newsgroup name="comp.lang.python">

?

Hope this helps a bit.


-- 
Matt Gushee
Portland, Maine, USA
mgushee at havenrock.com
http://www.havenrock.com/



More information about the Python-list mailing list