Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-07-12 04:15:55


David Abrahams wrote:
> on Wed Jul 11 2007, Mathias Gaunard
> <mathias.gaunard-AT-etu.u-bordeaux1.fr> wrote:
>
>> David Abrahams wrote:
>>
>>> I find XML horrible to read, however I find most of the
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> procedural
>>> code I've seen for manipulating it even more horrible.
>>>
>>> I would like to see a more declarative syntax for much of this
>>> stuff.
>>>
>>>
>>> root.push_front(
>>> tag("articleinfo")[
>>> title ? (comment("This title was moved"), title) : NULL
>>> , tag("author")[
>>> tag("firstname")["Joe"],
>>> tag("surname")["Random"]
>>> ]
>>> ]
>>> )
>>
>> Interesting. It doesn't look a lot like raw XML
>
> Maybe that's the point ;-)
>
>> but it certainly integrates better into C++.
>
> That too.

You're optimizing the wrong case, a toy example. It obviously depends on the
actual XML schema, but I typically average one Node::add_element call per
function when the format is sensible and can be mapped to C++.

void add_element( node * p, char const * n, ArticleInfo const & ai )
{
    node * p2 = p->add_element( n );
    add_element( p2, "author", ai.author() );
}

void add_element( node * p, char const * n, Author const & a )
{
    node * p2 = p->add_element( n );
    add_element( p2, "firstname", a.firstname() );
    add_element( p2, "surname", a.surname() );
}

void add_element( node * p, char const * n, std::string const & s )
{
    node * p2 = p->add_element( n );
    p2->add_text( s );
}

int main()
{
    ArticleInfo ai( ... );
    XmlDocument xd( "test.xml" );
    add_element( xd, "articleinfo", ai );
}

(this is C++ pseudocode and out of order but you get the point.)

This is how we can move to a list of authors:

void add_element( node * p, char const * n, ArticleInfo const & ai )
{
    node * p2 = p->add_element( n );
    add_element( p2, "authors", ai.authors() );
}

void add_element( node * p, char const * n, vector<Author> const & v )
{
    node * p2 = p->add_element( n );

    for( auto i = v.begin(); i != v.end(); ++i )
    {
        add_element( p2, "author", *i );
    }
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk