[XML-SIG] ANN: XMLBuilder 1.0

Uche Ogbuji uche.ogbuji at fourthought.com
Thu Aug 26 20:35:50 CEST 2004


On Wed, 2004-08-25 at 12:54, Mike Hostetler wrote:
> I read a good blog entry about a Builder object in Ruby [1] and I
> thought Python needed one.
> 
> Introducing XMLBuilder.  It's nothing special, but it works quite
> well.  You create an XMLBuilder object, send it some dictionary data,
> and it will generate the XML for you.  My version also allows nesting
> another XMLBuilder object inside, as well as adding them together
> (though that may not work like you want it to).
> 
> It's easier to show than to describe.  Here are some examples:
> 
> >>> from xmlbuilder import XMLBuilder
> >>> b2 = XMLBuilder()
> >>> b2.name = {"last":"flintstone", 'attr':{"type":"friend"}, "first":"fred"}
> >>> print b2
> <?xml version="1.0" ?>
> <name type="friend"><last>flintstone</last><first>fred</first></name>
> >>> b1.contacts = {"owner":"thehaas at binary.net",
> ...     "contact":b2}
> >>> print b1
> <?xml version="1.0" ?>
> <contacts><owner>thehaas at binary.net</owner><contact><name type="friend"><last>f\
> lintstone</last><first>fred</first></name></contact></contacts>
> >>> b = b1+b2
> >>> print b
> <?xml version="1.0" ?>
> <contacts><contacts><owner>thehaas at binary.net</owner><contact><name type="frien\
> d"><last>flintstone</last><first>fred</first></name></contact></contacts><name \
> type="friend"><last>flintstone</last><first>fred</first></name></contacts>

So out of curiousity, do people really prefer this sort of thing to the
(IMHO more straightforward) foo.createElement() type APIs available in
many other Python packages?

Side note, folks looking to generate XML may want to glance at

http://www.xml.com/pub/a/2002/11/13/py-xml.html
http://www.xml.com/pub/a/2003/10/15/py-xml.html
http://www.xml.com/pub/a/2003/04/09/py-xml.html
http://www.xml.com/pub/a/2003/11/12/py-xml.html

I shall give XMLBuilder the customary plug in my next column.  Thanks
for the effort.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Practical (Python) SAX Notes - http://www.xml.com/pub/a/2004/08/11/py-xml.html
XML circles the globe - http://www.javareport.com/article.asp?id=9797
Element structures for names and addresses - http://www.ibm.com/developerworks/xml/library/x-elemdes.html
Commentary on "Objects. Encapsulation. XML?" - http://www.adtmag.com/article.asp?id=9090
Harold's Effective XML - http://www.ibm.com/developerworks/xml/library/x-think25.html
A survey of XML standards - http://www-106.ibm.com/developerworks/xml/library/x-stand4/



More information about the XML-SIG mailing list