ElementTree is awesome

October 4th, 2006 § 0 comments

I’ve never had to do any amount of XML pars­ing — every­thing 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 seri­ously walking/consuming XML streams/files as part of a project.

At that time, I found ele­ment­tree to be the “eas­i­est” for me to under­stand. Admit­tedly, I was really ab(using) — I had some­thing to ship, and ship fast.

Well, all things come in cycles. I’m back to writ­ing a “con­sumer” library that con­sumes var­i­ous XML sources and returns some­thing “more human parseable” (build­ing tools for new python/programming peo­ple), and we’re back to elementree.

I know that in py2.5, it’s in the xml.etree pack­age, but we won’t be switch­ing off py2.4 for some time.

I started this mod­ule a day ago with typ­i­cal hack­ing 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 Ele­men­Tree docs, and a light­bulb 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 gen­er­a­tors, and some attrib/k,v items calls.

I can’t rec­om­mend the pack­age enough. For some­one like me not heav­ily versed in XML con­sump­tion the ease of use and power of Ele­ment­Tree can’t be stressed.

What's this?

You are currently reading ElementTree is awesome at jessenoller.com.

meta