Xml Generation Module

29 06 2007

This blog has moved to www.djangoandflex.org.uk

In all my apps I communicate client to server in XML. Flex has very nice XML processing capabilities through e4x. I typically generate only simple XML to send back to the server, where python’s processing is fairly easy too.

The last link in the chain is getting Django to generate XML easily. I wrote a very simple xml generating module that you might find useful.

It is designed to be as simple as possible. So you might have

>>> b = xml.book(
...     xml.title('The Title'), xml.author('Ian Millington')
...     )

and have it rendered to xml with:

>>> b.xml
'<book><title>The Title</title><author>Ian
Millington</author></book>'

Of course, things can get much more complex, it handles tag properties, namespaces, DTDs and so on. For example:

>>> svg = XMLNamespace('svg', 'http://www.w3.org/2000/svg')
>>> makeXhtml(html.html(html.body(svg.svg(), class='main-page')))

produces

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:svg="http://www.w3.org/2000/svg">
<body class="main-page">
<svg:svg/>
</body>
</html>

You can find the python module on google code here, with the actual python code here. The license is LGPL (i.e. no warranties, free for commercial use, non-copyleft).

The code is based on a HTML code generator I wrote as part of a research project last year. That in turn was inspired by the HTML creation system in MochiKit, which is in turn based on a Python HTML library, I believe, but I don’t know which one. Please fill me in on the missing inspiration if you know. In any case I think that the prior art used a fixed set of tag functions, rather than allowing any tags to be used. Correct me if I’m wrong!


Actions

Information

Leave a comment