Writing XML

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 6 14:57:57 PST 2011


On Sunday 06 February 2011 13:59:19 Rainer Schuetze wrote:
> Tomek Sowiński wrote:
> > auto xml = xmlWriter(outputRange);
> > 
> > xml.comment(books.length, " favorite books of mine.");
> > foreach (book; books) {
> > 
> >     xml.book("year", book.year, {
> >     
> >          foreach (author; book.authors) {
> >          
> >              xml.tight.authorName({
> >              
> >                  xml.first(author.first);
> >                  xml.middle(author.middle);
> >                  xml.last(author.last);
> >              
> >              });
> >          
> >          }
> >          xml.tight.title(book.title);
> >     
> >     });
> > 
> > }
> 
> This looks nice and compact Using opDispatch to specify the tag (I guess
> that is what you are using to create a tag "book" by calling xml.book())
> feels like misusing opDispatch, though. Does it add readability in
> contrast to passing the tag as a string to some function?
> 
> How do you write a tag named "tight"? Or a tag calculated at runtime?
> 
> Something more conventional would be
> 
> 	xml.tag("book", attr("year", book.year), { ...
> 
> but I'm not sure that pairing the attribute name and value adds
> readability or mere noise.

Actually, using opDispatch in that manner would become a big problem once you 
tried to have an xml tag with any function name that xml would have on it. It 
really doesn't sound like a good idea and really doesn't provide much benefit - 
if any - as far as I can see. It's so simple to just take the tag name as a 
string that I see no reason to do otherwise.

- Jonathan M Davis


More information about the Digitalmars-d mailing list