[xsd-users] How to preserve DOM when serializing?

Boris Kolpackov boris at codesynthesis.com
Tue Nov 20 13:33:12 EST 2007


Hi Ray,

Ray Lischner <rlischner at proteus-technologies.com> writes:

> We have a node with mixed-content. We use the keep_dom flag when parsing
> the object to preserve the mixed-content nodes. The object is later copied
> as an element of a larger object. The larger object was constructed in C++
> code, and lacks a DOM. We then want to serialize the larger object to a DOM;
> we want to keep the DOM for the inner element, but need to serialize all
> the other elements of the larger object.
>
> The first solution that I can think of is to customize the inner class so
> its operator<<(DOMElement*) function copies the DOM instead of serializing
> the object.

First of all, note that the DOM association is only maintained in
complete copies of the tree from the root node. This means that
when you copy the node into a larger object, your DOM association
will be gone.

There is a way to manually "associate" DOM to an object model that
was created programmatically (see the type::_node(DOMNode*) function).
However, this is a lot of trouble, especially if the object model is
big. An easier way, as you have suggested, is to customize the type
that needs to preserve its DOM content. But instead of using the
DOM association feature, I would simply store the DOM fragment
privately in this type within its own private DOMDocument. This
way you will customize the parsing c-tor to copy the DOM fragment
(importNode) into the private DOMDocument and the serialization
operator to copy the fragment into the output document. You will
also need to customize the copy c-tor to make proper copies of
the DOM fragment. This exact approach (private DOMDocument that
stores DOM fragments) is used in XSD 3.0.0 to handle wildcards.

Boris




More information about the xsd-users mailing list