Writing XML
spir
denis.spir at gmail.com
Sun Feb 6 15:32:11 PST 2011
On 02/06/2011 03:43 PM, Tomek Sowiński wrote:
> While I'm circling the problem of parsing, I took a quick look at writing not to get stuck in analysis-paralysis. Writing XML is pretty independent from parsing and an order of magnitude easier to solve. It was perfect to get myself coding.
>
> These are the guidelines I followed:
>
> * Memory minimalism: don't force allocating an intermediate node structure just to push a few tags down the wire.
>
> * Composability: operating on an arbitrary string output range.
>
> * Robustness: tags should not be left open, even if the routine producing tag interior throws.
>
> * Simplicity of syntax: resembling real XML if possible.
>
> * Space efficiency / readability: can write tightly (without indents and newlines) for faster network transfer and, having easy an means for temporary tight writing, for better readability.
>
> * Ease of use:
> - automatic to!string of non-string values,
> - automatic string escaping according to XML standard,
> - handle nulls: close the tags short (<tag/>), don't write attributes with null values at all.
>
> * anything else?
>
>
> The new writer meets pretty much all of the above. Here's an example to get a feel of it:
>
> auto books = [
> Book([Name("Grębosz", "Jerzy")], "Pasja C++", 1999),
> Book([Name("Navin", "Robert", "N.")], "Mathemetics of Derivatives", 2007),
> Book([Name("Tokarczuk", "Olga")], "Podróż ludzi Księgi", 1996),
> Book([Name("Graham", "Ronald", "L."),
> Name("Knuth", "Donald", "E."),
> Name("Patashnik", "Oren")], "Matematyka Konkretna", 2008)
> ];
>
> auto outputRange = ... ;
> 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);
> });
> }
>
> --------------------------------- program output ---------------------------------
>
> <!-- 4 favorite books of mine. -->
> <book year="1999">
> <authorName><first>Jerzy</first><middle/><last>Grębosz</last></authorName>
> <title>Pasja C++</title>
> </book>
> <book year="2007">
> <authorName><first>Robert</first><middle>N.</middle><last>Navin</last></authorName>
> <title>Mathemetics of Derivatives</title>
> </book>
> <book year="1996">
> <authorName><first>Olga</first><middle/><last>Tokarczuk</last></authorName>
> <title>Podróż ludzi Księgi</title>
> </book>
> <book year="2008">
> <authorName><first>Ronald</first><middle>L.</middle><last>Graham</last></authorName>
> <authorName><first>Donald</first><middle>E.</middle><last>Knuth</last></authorName>
> <authorName><first>Oren</first><middle/><last>Patashnik</last></authorName>
> <title>Matematyka Konkretna</title>
> </book>
>
>
> Questions and comments?
When does one need to write by hand, in source, structured data needing to be
serialised into XML (or any other format)? In my (admittedly very limited),
such data always are outputs of some processing (if only reading from other file).
denis
--
_________________
vita es estrany
spir.wikidot.com
More information about the Digitalmars-d
mailing list