For Programmers: Free Programming Magazines  


Home > Archive > Smalltalk > September 2005 > XML Generation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML Generation
tjones5760@gmail.com

2005-09-06, 9:57 pm

In an application I'm writing for fun I needed to do some XML
generation in VisualWorks. I started by using the included framework:

|doc item attributes|
doc := XML.Document new.
attributes := Array with: (XML.Attribute name: 'version' value:
'0.91').
doc addNode: (XML.Element tag: 'rss' attributes: attributes elements:
nil).
doc root addNode: ((XML.Element tag: 'title') addNode: (XML.Text text:
'Wonder Blog')).
doc root addNode: ((XML.Element tag: 'link') addNode: (XML.Text text:
'http://wonderblog.net')).
doc root addNode: ((XML.Element tag: 'description') addNode: (XML.Text
text: 'a really blog')).
item := XML.Element tag: 'item'.
doc root addNode: item.
item addNode: ((XML.Element tag: 'title') addNode: (XML.Text text:
'Cucumbers: green enough for you?')).
item addNode: ((XML.Element tag: 'link') addNode: (XML.Text text:
'http://wonderblog.net/cukes.htm')).
item addNode: ((XML.Element tag: 'description') addNode: (XML.Text
text: 'Greener and leaner')).
doc

but I felt this was too verbose. So I developed a little code to let me
say things like this instead:

|doc|
doc := TimJones.XMLDocument new.
doc tag: #rss attribute: #version -> '0.91' contents: [
doc tag: #channel contents: [
doc tag: #title contents: 'Wonder Blog'.
doc tag: #link contents: 'http://wonderblog.net'.
doc tag: #description contents: 'a really blog'.
doc tag: #item contents: [
doc tag: #title contents: 'Cucumbers: green enough for you?'.
doc tag: #link contents: 'http://wonderblog.net/cukes.htm'.
doc tag: #description contents: 'Greener and leaner'.
]
]
].
doc asXML

Maybe not a huge difference, but I prefer the latter - there's less
typing if nothing else. What do you think? How do you generate XML?

ps - I understand having a full object model can be a win in certain
circumstances, but here I just wanted to output some XML

--
tim jones

Chris Uppal

2005-09-07, 7:57 am

tjones5760@gmail.com wrote:

> Maybe not a huge difference, but I prefer the latter - there's less
> typing if nothing else. What do you think? How do you generate XML?


I agree. I would certainly either avoid, or hide, the complexities of "full
objects" if all I was interested in was creatng XML as a simple linear process.

Personally, I think I'd be more likely to use an explicitly stream-like
interface, but that's just a matter of preference (and there's very little
difference anyway).

-- chris


Steven Kelly

2005-09-07, 7:57 am

<tjones5760@gmail.com> wrote in message
news:1126055201.781687.103970@f14g2000cwb.googlegroups.com...
> In an application I'm writing for fun I needed to do some XML
> generation in VisualWorks. ...I developed a little code to let me
> say things like this instead:
>
> |doc|
> doc := TimJones.XMLDocument new.
> doc tag: #rss attribute: #version -> '0.91' contents: [


Just as an aside: if you're trying to make a blog server, you might
want to take a look at Silt:
http://www.cincomsmalltalk.com/CincomSmalltalkWiki/Silt
and/or the BottomFeeder aggregator:
http://www.cincomsmalltalk.com/BottomFeeder

All the best,
Steve
--
Steven Kelly, CTO, MetaCase,
http://www.metacase.com/blogs/stevek/blogView
"The most significant innovation over the next 10 years":
Bill Gates on Domain-Specific Modeling,
www.adtmag.com/article.asp?id=9166


tjones5760@gmail.com

2005-09-07, 7:01 pm

Steve,

I'm not writing this because there's a real need. :) I want to write
most of the software I use on a daily basis just to become a better
developer.

Regards,
Tim

tjones5760@gmail.com

2005-09-07, 7:01 pm

Chris,

If you've got the time/interest, could you give an exmaple of what that
would look like? How would you handle nested constructs (e.g. this
elements contains that element, which contains another element)?

Regards,
Tim

Doug Swartz

2005-09-10, 6:59 pm


On 7-Sep-2005, tjones5760@gmail.com wrote:

> If you've got the time/interest, could you give an exmaple of what that
> would look like? How would you handle nested constructs (e.g. this
> elements contains that element, which contains another element)?


We stream objects to XML quite a lot.

Here's a sample from my imagination for streaming a mythical Person object
to XML. The method names are quite close to what we use, but I didn't
actually pull up an IDE to verify them.

Person>>putXMLon: anXMLWriteStream
anXMLWriteStream
nextPutStartTag: self xmlIDtag
attributes: (Array with: 'id' -> self id).
anXMLWriteStream
nextPutAll: self name
tag: 'name'.
anXMLWriteStream
nextPutAllEach: self hobbies
tag: 'hobby'
withinTag: 'hobbies'.
self address putXMLon: anXMLWriteStream.
anXMLWriteStream nextPutEndTag: self xmlIDtag


This sample shows a couple of different ways to create the nested
constructs: a simple list of hobbies, and a more complex structure where
the Address object has its own putXMLOn: method. I've foiund that if you
need to nest more than a single layer deep, you probably already have an
object to delegate the responsibuility to.

XMLWriteStream understands other methods, of course: nextPutStartTag:
(without attributes), nextPutAll:tag:attributes:,nextPutAllCda
ta:,
nextPutAllComment: etc.

This relatively simplistic approach has worked well for us. It's pretty
readable and seems to handle even complicated object models well.

Doug Swartz
tjones5760@gmail.com

2005-09-11, 3:56 am

This is my current favorite approach:

|doc|
doc := TimJones.XMLDocument new.
doc tag: #rss attribute: #version -> '0.91' contents: [
doc channel: [
doc title: 'Wonder Blog'.
doc link: 'http://wonderblog.net'.
doc description: 'a really blog'.
doc item: [
doc title: 'Cucumbers: green enough for you?'.
doc link: 'http://wonderblog.net/cukes.ht m'.
doc description: 'Greener and leaner'.
]
]
].
doc asString

Inspired by the way ruby does it (and a number of other smalltalkers
too) using #doesNotUnderstand:....

Fernando Rodriguez

2005-09-11, 7:56 am

On 10 Sep 2005 22:32:11 -0700, tjones5760@gmail.com wrote:


>
>Inspired by the way ruby does it (and a number of other smalltalkers
>too) using #doesNotUnderstand:....


Could you elaborate on this?

Thomas Gagne

2005-09-12, 3:57 am

Most recently, my favorite way to create XML is using SSP pages through
the built-in web server. This is especially convenient if the client
can easily access the XML via HTTP.

In May and June of 2004 there was a thread subject, "XML's a drag. Can
I use SSPs inside a program?" During the thread there were hints of an
AR suggesting a generalized templating method that would allow programs
to build stream (text) as easily as they can be built using .ssp pages
but without the overhead of the web server and restrictions requiring
the template source be a file.

Were this available I believe your job would have been done ws ago.
Perhaps you can get some of the workarounds suggested in the thread to
work for you.
Thomas Gagne

2005-09-12, 3:57 am

I should have been more specific about where the conversation took
place. It was on the VWNC list.

The start of the thread is here
<http://www.parcplace.net/list/vwnc-...5/msg00100.html>
Ian Upright

2005-09-17, 7:01 pm

tim jones (tjones5760@gmail.com)wrote:

>|doc|
>doc := TimJones.XMLDocument new.
>doc tag: #rss attribute: #version -> '0.91' contents: [
> doc tag: #channel contents: [
> doc tag: #title contents: 'Wonder Blog'.
> doc tag: #link contents: 'http://wonderblog.net'.
> doc tag: #description contents: 'a really blog'.
> doc tag: #item contents: [
> doc tag: #title contents: 'Cucumbers: green enough for you?'.
> doc tag: #link contents: 'http://wonderblog.net/cukes.htm'.
> doc tag: #description contents: 'Greener and leaner'.
> ]
> ]
>].
>doc asXML
>
>Maybe not a huge difference, but I prefer the latter - there's less
>typing if nothing else. What do you think? How do you generate XML?
>
>ps - I understand having a full object model can be a win in certain
>circumstances, but here I just wanted to output some XML


I developed something similar to your approach above.. It's kind of a smart
state machine, in that depending on when or where you are closing tags,
elements, etc. depends on what (or how verbose) XML gets emitted. It just
uses a stream and pushes data out to it immediately. It's really not that
complicated.

My framework doesn't go near VW's XMLDocument, and it emits the XML at least
three times as fast as building a VW XML document in it's entirety and then
dumping it out. This is especially true for extremely large XML files, in
the hundreds of megabytes or more. In the gigabytes range on a machine with
little ram, obviously, the XMLDocument approach is just not going to cut it
at all.

Unfortunately the XML emitting framework I developed is owned by a company
so I can't release it.

Ian

---
http://www.upright.net/ian/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com