Jonas Galvez wrote:
Not sure if it's been done before, but still...
from __future__ import with_statement
from xmlbuilder import builder, element
xml = builder(version="1.0", encoding="utf-8")
with xml.feed(xmlns='http://www.w3.org/2005/Atom'):
xml.title('Example Feed')
xml.link(None, href='http://example.org/')
xml.updated('2003-12-13T18:30:02Z')
with xml.author:
xml.name('John Doe')
xml.id('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6')
with xml.entry:
xml.title('Atom-Powered Robots Run Amok')
xml.link(None, href='http://example.org/2003/12/13/atom03')
xml.id('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a')
xml.updated('2003-12-13T18:30:02Z')
xml.summary('Some text.')
print xml
Will produce:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<link href="http://example.org/" />
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03" />
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
</feed>
http://github.com/galvez/gae-rest/tree/258066f5e1a32c999e04a9313943fdfa8e64edd9/xmlbuilder.py
--Jonas Galvez
--
http://mail.python.org/mailman/listinfo/python-list
Nice! Here's a version that uses elementtree:
8<----------------------------------------------------
from __future__ import with_statement
from xml.etree import ElementTree as ET
class element(object):
def __init__(self, name, parent):
self.parent = parent
self.element = ET.SubElement(parent, name)
def __str__(self):
return ET.tostring(self.parent)
def __getattr__(self, name):
return element(name, self.parent)
def __getitem__(self, name):
return element(name, self.parent)
def __enter__(self):
self.parent = self.element
return self
def __exit__(self, type, value, tb):
pass
def __call__(self, value='', **kargs):
self.element.text = value
self.element.attrib = kargs
return self
class builder(element):
def __init__(self, version, encoding):
self.parent = ET.Element('root')
if __name__ == "__main__":
xml = builder(version="1.0", encoding="utf-8")
with xml.feed(xmlns='http://www.w3.org/2005/Atom') as feed:
feed.title('Example Feed')
feed.link(href='http://example.org/')
feed.updated('2003-12-13T18:30:02Z')
with feed.author as author:
author.name('John Doe')
feed.id('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6')
with feed.entry as entry:
entry.title('Atom-Powered Robots Run Amok', class_='l')
entry.link(href='http://example.org/2003/12/13/atom03')
entry.id('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a')
entry.updated('2003-12-13T18:30:02Z')
entry.summary('Some text.')
with feed.entry as entry:
entry.title('2')
entry.link(href='2')
entry.id('2')
entry.updated('2')
entry.summary('2')
print xml
8<----------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list