[4suite] Article: Gems From the [XML-SIG] Archives
Craeg K Strong
cstrong@arielpartners.com
Thu, 10 Apr 2003 10:16:19 -0400
>
>
> If you are interested in a bit of a mind-bending experimentation in
> XML output using Python idioms, see this message
> <http://mail.python.org/pipermail/xml-sig/1998-October/000423.html> by
> Greg Stein. As an example of the very interesting perspective it
> provides, the following snippet should generate a bit of XHTML:
>
>|f = Factory()
>body = f.body(bgcolor='#ffffff').p.a(href='l.html').img(src='l.gif')
>html = f.html[f.head.title('title'), body] |
>
> I think you'll agree this is delightfully twisted.
>
Good catch! The above seems to me a quite natural and Pythonic way to
produce XML.
It reminds me of Jakarta's Element Construction Set (
http://jakarta.apache.org/ecs ) which we
have been using to produce XML from Java for some time:
Html html = new Html()
.addElement(new Head()
.addElement(new Title("Demo")))
.addElement(new Body()
.addElement(new H1("Demo Header"))
.addElement(new H3("Sub Header:"))
.addElement(new Font().setSize("+1")
.setColor(HtmlColor.WHITE)
.setFace("Times")
.addElement("The big dog & the little cat chased each other.")));
out.println(html.toString());
Of course, Python's built in introspection capabilities make it much
more terse. Yay, Python.
--Craeg
Uche Ogbuji wrote:
> http://www.xml.com/pub/a/2003/04/09/py-xml.html
>
> "In this and in subsequent articles I will mine the richness of the
> XML-SIG mailing list for some of its choicest bits of code. I start in
> this article with a couple of very handy snippets from 1998 and 1999.
> Where necessary, I have updated code to use current APIs, style, and
> conventions in order to make it immediately useful to readers. All
> code in this article was tested using Python 2.2.1 and PyXML 0.8.2."