[Python-ideas] xml generator that uses the with statement

Gerard flanagan grflanagan at gmail.com
Mon Sep 15 00:49:46 CEST 2008


Mathias Panzenböck wrote:
> Here is a quick and dirty draft of a xml generator that uses the with statement:
> http://twoday.tuwien.ac.at/pub/files/XmlMarkup (ZIP, 3 KB)
> 
> It is inspired by rubies XmlMarkup class.
> 
> Brief usage:
>>>> from __future__ import with_statement
>>>> from XmlMarkup  import *
>>>> import sys
>>>>
>>>> with XmlMarkup(sys.stdout) as xm:
>>>> 	with xm.root:
>>>> 		xm.text('foo')
>>>> 		with xm.prefixMapping('x','http://example.com/x'):
>>>> 			with xm.tag.ns('http://example.com/x'):
>>>> 				xm.comment('comment')
>>>> 			with xm['text']:
>>>> 				xm.text('bar')
>>>> 		with xm.tag(foo='bar',egg='spam'):
>>>> 			pass
> <?xml version="1.0" encoding="utf-8"?>
> <root>foo<x:tag
> xmlns:x="http://example.com/x"><!--comment--></x:tag><text>bar</text><tag
> foo="bar" egg="spam"></tag></root>
> 
[...]
> 
> What do you think? :)
> 
> 
> 	-panzi

Something similar, but without namespace handling:

http://article.gmane.org/gmane.comp.python.general/579900/

(I like that you have a class called 'maybe' :-)

Regards

Ger




More information about the Python-ideas mailing list