« Welcome | Main | Sending Images From Flex to a Server »

Xml Generation Module

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 might proove 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!

EDIT: The module bears a striking resemblance to XIST (thanks for the pointer Tom!). XIST seems to be slightly more complex to get going, but lots more capable too.

TrackBack

TrackBack URL for this entry:
http://www.icosagon.com/mt/mt/mt-tb.cgi/163

Comments

Good stuff, thanks. I think the library you're after is XIST.

great blog, I am using django+flex myself - for charts!

why don't you use minidom or elementtree that are part of the python distribution?

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)