Writing XML

Rainer Schuetze r.sagitario at gmx.de
Sun Feb 6 13:59:19 PST 2011


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.

Rainer


More information about the Digitalmars-d mailing list