|
Boost : |
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2007-07-11 14:23:25
David Abrahams wrote:
> on Fri Jul 06 2007, Stefan Seefeld <seefeld-AT-sympatico.ca> wrote:
>
>> over the last couple of years we have discussed possible XML APIs
>> for inclusion into boost. As I already had an early prototype for
>> such an API, I kept evolving it, based on feedback from those
>> discussions.
>> A couple of weeks ago I actually checked it into the sandbox
>> (http://svn.boost.org/trac/boost/browser/sandbox/xml). Today,
>> I adjusted the source layout to conform to the sandbox layout
>> we agreed on, including a boost.build - based build-system.
>>
>> I would appreciate if anybody interested into a future boost.xml
>> submission would have a look, provide feedback, or even get
>> involved into the (ongoing) development.
>
> Okay, this is going to sound very opinionated:
>
> I find XML horrible to read
Agreed; I think it's just sufficiently good that a better alternative
would fail.
> however I find most of the procedural code I've seen for manipulating it even more horrible.
You've seen my code, haven't you :-)
> I would like to see a more declarative syntax for much of this stuff.
I prefer to read any constant fragments from files or from literal
strings, which is about as declarative as you can get.
> root.push_front(
> tag("articleinfo")[
> title ? (comment("This title was moved"), title) : NULL
> , tag("author")[
> tag("firstname")["Joe"],
> tag("surname")["Random"]
> ]
> ]
> )
How close can we get to that using Boost.Assign, if the element class
is a model of a suitable container?
But I think that using subclasses is the key to making this sort of
code easier on the eye:
struct firstname: public element {
firstname(n): element("firstname",n) {};
};
etc.
articleinfo ai;
if (title) ai.push_back(comment("This title was moved"));
author a;
a.push_back(firstname("Joe"));
a.push_back(surname("Random"));
ai.push_back(a);
root.push_back(ai);
Just adding some non-standard indentation to that makes it almost as
clear as I need. You don't need the {}, but it keeps your editor's
auto-indent happy:
articleinfo ai;
{
if (title) ai.push_back(comment("This title was moved"));
author a;
{
a.push_back(firstname("Joe"));
a.push_back(surname("Random"));
ai.push_back(a);
}
root.push_back(ai);
}
You can also reduce the declare-populate-add to delcare-populate if you
pass the parent to the sub-elements' constructors:
articleinfo ai(root); {
if (title) comment(ai,"This title was moved");
author a(ai); {
firstname(a,"Joe");
surname(a,"Random");
}
}
Of course there is a question of the lifespan of these
apparently-temporary objects to resolve. Anyway, I think that
nice-looking XML-generating code can be possible without "resorting to"
operator overloading.
Regards,
Phil.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk