Announcing XMLegant for Python
I've written a Python version of XMLegant, which I released earlier this morning for PHP. The Python version was designed to be as compatible as possible with the PHP version, without unnecessarily abusing the language.
XMLegant (github, download latest as zip) is a tool designed to allow easy generation of XML.
To see how XMLegant works, let's take a simple example. Suppose we want to generate the following document:
<books>
<book>
<title>Title 1</title>
<author>Author 1</author>
<isbn>isbn 1</isbn>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
<isbn>isbn 2</isbn>
</book>
<book>
<title>Title 3</title>
<author>Author 3</author>
<isbn>isbn 3</isbn>
</book>
</books>
We can use the following code:
x = XMLegant()
for i in xrange(5):
x.books.book().title("Title %d" % i) \
.author("Author %d" % i) \
.isbn("isbn %d" % i)
And to generate this document:
<a>
<b>c</b>
<b>d</b>
<e>f</e>
<b>g</b>
<h>
<i j="k"/>
<l>
<m/>
</l>
<n>o</n>
</h>
<a>
We can use either:
x = XMLegant()
x.a.b('c')
x.a.b('d')
x.a.e('f')
x.a.b('g')
x.a.h.i['j'] = 'k'
x.a.h.l.m
x.a.h.n = 'o'
or:
x = XMLegant()
x.a() \
.b('c') \
.b('d') \
.e('f') \
.b('g') \
.h() \
.i('j', 'k') \
.l() \
.m('') \
.getParent() \
.n('o')
A walkthrough of a short example is available in the XMLegant for PHP post. Many more examples are available in demo.py. Also, XMLegant_Tests.py consists of a number of unit tests that may be useful as a guide.