Easyxml is a project mainly written in Python, based on the MIT license.
Concise XML generation in Python
EasyXML is an easy and concise way to generate XML output in Python. It uses a custom attribute getter so element names can be specified directly in the code:
books = EasyXML('books') books.book(title='Example A') books.book.author(name='John Smith', age=57) books.book.publisher(name='Publisher A') books.book(title='Example B') books.book.author(name='Jane Doe', age=30) books.book.author(name='James Cutter', age=45) books.book.publisher(name='Publisher B') print str(books)
The above code produces the following XML:
You don't actually need to create every parent element, allowing the following code to work:
root = EasyXML('root') root.a.b.c() root.a.b.c() root.a() root.a.b.c() root.a.b.c() print str(root)
The above code produces the following XML:
Creating an element returns that element, which can then be passed to helper methods:
def material(primitive, ambient, diffuse): primitive.ambient(r=ambient[0], g=ambient[1], b=ambient[2]) primitive.diffuse(r=diffuse[0], g=diffuse[1], b=diffuse[2])
root = EasyXML('root') material(root.primitive(type='sphere'), (64, 0, 0), (192, 0, 0)) material(root.primitive(type='cube'), (0, 64, 0), (0, 192, 0)) print str(root)
The above code produces the following XML: