python and XML

markup.py does approximately what XML::Generator does, but in an easier (simpler) way.

I still prefer SimpleXML or almost even Castor or XMLBeans.

Markup may not be good enough for building RESTful web services, but I’m toying with switching the prototype ShoppingList web service over to python –just for fun. Building (and iterating) through complex hierarchies in python *is* nice. Nicer even than perl.

Anyway:

import markup

items = ( "Cookies", "Milk", "Apples" )  

doc = markup.page("xml") 
doc.shoppinglist() 
doc.items() 
i = 0 
for item in items:
    i = i + 1
    doc.item(item, id_=i, qty_=1)
doc.items.close()
doc.shoppinglist.close()

print doc

yields:

<shoppinglist>
<items>
<item id="1" qty="1">Cookies</item>
<item id="2" qty="1">Milk</item>
<item id="3" qty="1">Apples</item>
</items>
</shoppinglist>

One thought on “python and XML

Leave a comment