On Monday, April 23, 2007, 3:54:44 PM, Sébastien Arnaud wrote: > Just like any other dev on the block I took RoR for a test drive to > see what the fuss was all about, but short of sharing my experience > with it and what I liked or disliked about it, I wanted to know if > there is a close equivalent to the RXML/Builder Ruby lib that saw the > light under the RoR project: > http://builder.rubyforge.org/
> To me it is the best part of the Rails framework :), but I can't seem > to find a simple and syntactically attractive module in python that > achieves the same purpose. I posted below a quick naive example for > those who are not familiar with ruby-builder lib. If you're just comparing how attractive the syntax is, that sounds more like a python v. ruby discussion, rather than an XML-SIG discussion. Normally in the python world the goal is to be as pythonic as possible, which is, of course, beautiful in its own right. :) > Thank you for any type of pointers! If I can't find anything then I > might decide to start to write my own python based module on RXML/ > Builder philosophy. <snipping ruby code> > Output > ====== > <?xml-stylesheet href="/xsl/mytext.xsl" type="text/xsl"?> > <mytestdoc> > <testing world="Escape me < > !!!">Hello World!</testing> > <person> > <comment>Jim's dog is nice & friendly</comment> > <name>Jim</name> > </person> > </mytestdoc> Since ElementTree is included in python 2.5, I'd always give that a shot, first: # use xml.etree.ElementTree for a pure python implementation import xml.etree.cElementTree as ET # Create the root element mytestdoc = ET.Element('mytestdoc') # Attributes can be created with dictionaries or keyword arguments # Add text with <object>.text testing = ET.SubElement(mytestdoc, 'testing', {'world': 'Escape me < > !!!'}) testing.text = 'Hello World!' person = ET.SubElement(mytestdoc, 'person') # You can add text directly to new subnodes ET.SubElement(person, 'comment').text = "Jim's doc is nice & friendly" ET.SubElement(person, 'name').text = 'Jim' # For testing, dump out the text to stdout ET.dump(mytestdoc) # Or, you can write it to a file tree = ET.ElementTree(mytestdoc) tree.write(r'C:/temp/mytestdoc.xml') This gives you: <mytestdoc><testing world="Escape me < > !!!">Hello World!</testing><person><comment>Jim's doc is nice & friendly</comment><name>Jim</name></person></mytestdoc> ElementTree doesn't put in the xsl-stylesheet line, and it doesn't prettyprint. http://effbot.org/zone/element-lib.htm has a short function for that. -- Best regards, Andrew Diederich _______________________________________________ XML-SIG maillist - XML-SIG@python.org http://mail.python.org/mailman/listinfo/xml-sig