[xsd-users] Determine parent/child element in C++/Tree

Boris Kolpackov boris at codesynthesis.com
Fri Jun 5 13:03:33 EDT 2015


Hi Robert,

Robert Krajewski <robert.w.krajewski at gmail.com> writes:

> I have the following problem. I need to determine the parent element of an
> element.
> E. g.
> <A>
>   <C />
> </A>
> 
> and
> <B>
>   <C />
> </B>
> 
> Is a given C a child of A or B? 

First of all, the C++/Tree object model doesn't deal in term of elements
but rather in terms of data members. In other words, once the object
model is constructed, there is no longer any knowledge (well, with a
few exceptions; see below) that there was an XML element called "C".
We now have an object model node that is of a type of element "C" and
which you can access using a C++ data member accessors.

So, if what you really need is to find out whether object node
corresponding to element "C" is contained by an object node
corresponding to element "A" or "B", then, assuming A and B
have different types, you can do it like this (*_t denotes
types of the elements):

C_t& c = ...;

xml_schema::type* x = c._container ();

if (x == nullptr)
  // Not contained by anybody.
else if (A_t* a = dynamic_cast<A_t*> (x))
  // A.
else if (B_t* b = dynamic_cast<B_t*> (x))
  // B.
else
  // Something else.

Now if what you are really after are XML elements/names, then probably
the best way to accomplish this would be with the DOM Association feature
(see the manual).

Boris



More information about the xsd-users mailing list