ElementTree is awesome

by jesse in ,


I've never had to do any amount of XML parsing - everything I have done has come from "seat of the pants" grepping/str().search/etc. A few months ago, I had to break down and start seriously walking/consuming XML streams/files as part of a project. At that time, I found elementtree to be the "easiest" for me to understand. Admittedly, I was really ab(using) - I had something to ship, and ship fast.

Well, all things come in cycles. I'm back to writing a "consumer" library that consumes various XML sources and returns something "more human parseable" (building tools for new python/programming people), and we're back to elementree.

I know that in py2.5, it's in the xml.etree package, but we won't be switching off py2.4 for some time.

I started this module a day ago with typical hacking gusto:

tree = ElementTree.fromstring(xml) for node in self.tree.getchildren(): ...parse code for x in node.getchildren(): for y in x.getchildren():

...And so on. It was a mess. Then I re(read) the ElemenTree docs, and a lightbulb went off. With things like tree.attrib, tree.getiterator("node"), and the like I compressed/ditched a bunch of slow, unwieldy (and gross) and replaces it with a few generators, and some attrib/k,v items calls.

I can't recommend the package enough. For someone like me not heavily versed in XML consumption the ease of use and power of ElementTree can't be stressed.