This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Writing XML in Smalltalk code
Feed Title: Michael Lucas-Smith
Feed URL: http://www.michaellucassmith.com/site.atom
Feed Description: Smalltalk and my misinterpretations of life
An interesting idea that has been popping up over and over again in the Smalltalk community is writing out XML or HTML using Smalltalk methods. I've seen it in Seaside and AIDA/Web, so I thought I'd throw in my own bone for fun.
So I ended up with something like the following:
XML.Document asMaker html: [:html |
html head: [:head |
head title: [:title | title + 'My Title'].
head body: [:body |
body + 'Hello World.'.
body br.
body + 'How are you?']].
Pretty simple huh? And it's all done using DoesNotUnderstand in Smalltalk. Instead of having to model all the different sorts of HTML and other XML's as is done in Seaside and AIDA/Web, this is just one class XMLMaker plus three class extensions to add some helper methods.
It also does namespaces:
XML.Document asMaker html: [:html |
html xmlns: 'http://www.w3.org/xhtml'.
html xmlns_foo: 'http://www.foo.com'.
html foo_bar: [:bar |
bar + 'Inside the bar in the foo namespace'].
html body: [:body |
body + 'Inside the body in the xhtml namespace']].
Check it out, it's XMLMaker in the public store repository. It works with a plain XML parser as well as the WithStyle Specialisation code if you have it loaded.