[xsd-users] keeping annotations/comments in xml file

Boris Kolpackov boris at codesynthesis.com
Wed Aug 22 05:32:11 EDT 2007


Hi Inna,

Inna Grois <inna at hyperroll.com> writes:

> I would like to update xml file while keeping the annotations if they
> exist in xml.
>
> Is it possible?

There is no automatic way to preserve comments in the resulting
documents. The serialization operators remove any child nodes
(including comments) from DOM elements before creating new
content.

The straightforward way to achieve what you want would probably
be to traverse the old DOM document and copy any comments to
the correct places in the new one. The tricky part could be
figuring out when to copy a comment and when not to. Here
is a quick example to illustrate what I am talking about.
Let's say you start with the following document:

<person>
  <!-- Record for John -->
  <name>John Doe</name>
  <age>21</age>
</person>

Now you load it into the object model, modify the data and
write it out to the updated document (in DOM):

<person>
  <name>Jane Doe</name>
  <age>18</age>
</person

The problem is, how do you determine whether to copy the comment
from the old document or not.


Another, more elegant, way to do this would be to customize the
xml_schema::type type (a base type for all generated classes)
with a new DOMElement serialization operator that will copy
the comments from the old DOM tree (that should be associated
with the object model using the keep_dom flag) to the new one.


hth,
-boris




More information about the xsd-users mailing list