Boost logo

Boost :

Subject: Re: [boost] xml?
From: Maarten L. Hekkelman (m.hekkelman_at_[hidden])
Date: 2010-04-08 11:56:35


Op 08-04-10 17:22, Phil Endecott schreef:
> Very interesting.
>
Thanks.
> Just one observation for now. I see that you have a node class that
> points to its neighbours and parent. I think this is quite a common
> pattern for XML, but it differs from e.g. the standard containers. So
> it's not possible to use standard algorithms to iterate through your
> XML structure.
>
I started with containers, but ended up with the current structure since
it is so much more practical.

You can still iterate in container style over children though. The
member 'children()' of a node returns a std::list of pointers to
children nodes. Therefore you can write code like:

foreach (xml::element* e, node->children<xml::element>())
     ;

As I said, I started with simply nodes that had children in a member STL
list. This seemed the /right/ way to do it. But then I ran into problems
when I had to create processing instruction nodes, comment nodes and
even attribute nodes. These are needed for a correct (and
straightforward) xpath implementation. So I had to create a common base
class, node, and element now is a subclass of node.

Fortunately, not all is lost. The children method above can be used for
traversal, but it is even more convenient to use xpath.evaluate() to
create a selection and iterate that:

foreach (xml::element* e, xpath("//my-node").evaluate<xml::element*>(n))
     ;

Or even (as a shortcut)

foreach (xml::element* e, xmlDoc->find("//my-node"))
     ;

The find method of element always returns an set of elements, stripping
out the other nodes.

Hope this explains the design a bit.

-maarten


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