Datacentric XML
Saturday, September 4th, 2004As I mentioned last post, we like to use the STL to process our XML. This really only works with datacentric XML documents. What I mean by datacentric XML, is XML that tends to hold collections of XML “records” with the same structure. An example would look like this:
<customers>
<customer>
<name>John Doe</name>
<address>
<street-address>123 N. Main</street-address>
<city>Beverly Hills</city>
<state>California</state>
<zip>90210</zip>
</address>
<comment>Rich guy</comment>
</customer>
<customer>
<name>Jane Doe</name>
<address>
<street-address>1235 Hill Ave</street-address>
<city>Beverly Hills</city>
<state>California</state>
<zip>90210</zip>
</address>
<comment/>
</customer>
</customers>
Given that XML tree, we could ask for a collection of customers, and using a std::sort, sort on zipcode. Then we could use std::for_each and a custom function to read each customer tree and populate a mail-merge app for a monthly mailing list.
We could also create a custom function that decorates the address tree with 5+4 digit zip codes. We could then get a collection of the zip code nodes. Using std::transform, and the function, we could iterate over each of these nodes, converting their zip codes, then, reinserting them into the tree, perhaps as a new node called <full-zip>.
Using datacentric XML can provide flexible data storing needs. We’ve found it to be an excellent solution for handling data for several different design patterns. Especially the decorator, and command patterns.