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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s