26 Mar 2009 movement   » (Master)

Outputting XML in standard Python

Is it really this ugly? I expected something like this:


doc = xmldoc()
doc.start('foo', { 'id': 'blah' })
doc.start('sub')
doc.text('subtext')
doc.close('sub')
doc.close('foo')
print doc


and I thought I had it in SimpleXMLWriter. However, I have to jump hoops to get it to output to a string, and it doesn't have any pretty-print. I tried using ElementTree, but that also doesn't pretty print! libxml2 is horribly low-level. lxml seems to do pretty printing, but it's still just as ugly as the best option I've found so far, xml.dom.minidom:


from xml.dom.minidom import Document
foo = doc.createElement('foo')
foo.setAttribute('id', 'blah')
doc.appendChild(foo)
sub = doc.createElement('sub')
sub.appendChild(doc.createTextNode('subtext'))
foo.appendChild(sub)


Yuck! If I'm building up a document, I almost always want to append directly at the last point: why do I have to keep track of all these elements by hand? I presume I'm missing some small standard helper module, but #python didn't know about it. Anyone?

Syndicated 2009-03-26 02:43:00 (Updated 2009-03-26 03:20:31) from John Levon

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!