xml.parsers.expat loading xml into a dict and whitespace

kaens apatheticagnostic at gmail.com
Wed May 23 02:58:13 EDT 2007


Now the code looks like this:

import xml.etree.ElementTree as etree

optionsXML = etree.parse("options.xml")
options = {}

for child in optionsXML.getiterator():
    if child.tag != optionsXML.getroot().tag:
        options[child.tag] = child.text

for key, value in options.items():
    print key, ":", value

freaking easy. Compare with making a generic xml parser class, and
inheriting from it for doing different things with different xml
files. This does exactly the right thing. I'm sure it's not perfect
for all cases, and I'm sure there will be times when I want something
closer to expat, but this is PERFECT for what I need to do right now.

That settles it, I'm addicted to python now. I swear I had a little
bit of a nerdgasm. This is orders of magnitude smaller than what I had
before, way easier to read and way easier to maintain.

Thanks again for the point in the right direction, Steve.

On 5/23/07, kaens <apatheticagnostic at gmail.com> wrote:
> > [1] ElementTree is in the 2.5 standard library, but if you're stuck with
> > an earlier python, just Google for it -- there are standalone versions
>
> I've got 2.5, and I'm not attached to expat at all. I'll check it out, thanks.
>



More information about the Python-list mailing list