Ramblings

April 10, 2008

Simple XML creation with Python

Filed under: cool, dev, education, python, tip, tool, web2py — michaelangela @ 10:39 pm

I was so happy when I first found Amara. It was the easiest thing I had seen for creating XML with Python. It’s quite serious stuff because it can probably handle just about anything you throw at it. Then I heard about the wonderful web2py project. One of it’s beautiful aspects is XML generation, or any tag type of thing actually. Some of that discussion is here. Thing of beauty. I wanted to use web2py but because of time restraints, my current project will continue to use Django, which definitely isn’t a bad thing. I just understand web2py more intuitively. I thought about using the TAG but I didn’t want the extra database hit when importing TAG from the gluon.html library. :-(

So I decided to search for python simple xml thinking of PHP’s SimpleXML. Results with DOM, SAX or elementtree… or anything like that… automatically disqualified. Amara was better for me than those. Then I saw pyfo…

pyfo – Easy XML Generation from Python

This package was developed by Luke Arno for Central Piedmont Community College in response to dissatisfaction with available alternatives for quickly generating XML. Concatenating strings is ugly and error-prone, using OO APIs for XML is heavy and overkill for generating simple XML output.

No kidding!

In [16]: from pyfo import pyfo

In [17]: a=(‘test’,(‘test’,’more stuff’,{‘cool':’beans’}))

In [18]: pyfo(a)
Out[18]: u'<test><test cool=”beans”>more stuff</test></test>’

In [19]: print pyfo(a, pretty=True, prolog=True)
<?xml version=”1.0″ encoding=”utf-8″?>
<test>
<test cool=”beans”>more stuff</test>
</test>

Does it get any easier than that?!

About these ads

1 Comment »

  1. This is awesome. I was looking for such a simple xml handling library.

    Comment by mnuhoglu — July 1, 2010 @ 7:46 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Google+ photo

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

Connecting to %s

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: