This is

as days pass by, by Stuart Langridge

. Here I write about many things. In the past I wrote about other things but the past is past. I write code for people to play with, I write about my life on Twitter, and I write here.

On I wrote Python and XML, on the subject of Rants and Software.

Lots of noise about Python’s vast plethora of XML libraries from Nelson and responses from Uche. I still think it’s too complicated. Way too complicated. What I want to do is this:

>>> import xmllib
>>> xmldoc = xmllib.open("filename.xml")
>>> print xmldoc.xpath("//node1/node2")
[<DOM Element: node2 at 0x3457>, <DOM Element: node2 at 0x090532>]
>>> print xmldoc.dom.getElementsByTagName("node2")
[<DOM Element: node2 at 0x3457>, <DOM Element: node2 at 0x090532>]
That’d do me. In fact, I think I might write it.
Jason

A bit like rexml for ruby?

Andy Todd

Isn’t that pretty much what the effbot’s ElementTree package does?

>>> from elementtree import ElementTree
>>> xmldoc = ElementTree.parse(‘filename.xml‘)
>>> print xmldoc.findall(‘//node1/node2‘) – Output left out because I’m lazy
>>> print xmldoc.getiterator(‘node2‘) – Ditto with the output

The API may not be exactly what you are after (there is nary a mention of DOM or SAX) but it’s pythonic, compact and blazingly fast.

sil

Hm. Must look at ElementTree, although it doesn’t seem very DOMish, and I tend to think in DOM terms as well as XPath terms…

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.