kaens wrote:
> 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
kaens wrote:
> Now the code looks like this:
>
[snip ElementTree code]
>
> 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
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,
> [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.
--
http://mail.python.org/mailman/listinfo/python-list
Ok, I can fix it by modifying
if self.inOptions and self.curTag != "options":
to
if self.inOptions and self.curTag != "options" and self.curTag != ""
but this feels really freaking ugly.
Sigh.
Any suggestions? I know I must be missing something.
Also, I hate the tendency I have to figure st
kaens wrote:
> Let's say I write a simple xml parser, for an xml file that just loads
> the content of each tag into a dict (the xml file doesn't have
> multiple hierarchies in it, it's flat other than the parent node)
[snip]
>
> hey
> bee
> eff
>
>
> it prints out:
> " :
>
> three : eff
> two
Wait. . . it's because the curTag is set to "", thus it sets the
whitespace after a tag to that part of the dict.
That doesn't explain why it does it on a xml file containing no
whitespace, unless it's counting newlines.
Is there a way to just ignore whitespace and/or xml comments?
On 5/23/07, k