[xsd-users] Problem with assymmetry in serialization/deserialization

Boris Kolpackov boris at codesynthesis.com
Thu Dec 13 02:39:14 EST 2012


Hi Robert,

Bielik, Robert <Robert.Bielik at gyros.com> writes:

> Hi all,
> 
> I'm trying to use template functions to make for transparent serialization/deserialization, however, without -generate-element-map I can only make deserialization work through:
> 
> template<typename ObjectType>
> std::auto_ptr<ObjectType> getTypedObject(DOMDocument* doc)
> {
>     std::auto_ptr<ObjectType> ret;
>     try
>     {
>         ret.reset(new ObjectType(*doc->getDocumentElement()));
>     }
>     catch (std::exception&)
>     {
>         // Catch all
>     }
>     return ret;
> }
> 
> which works nicely. 

> However serializing is a whole other matter. To serialize the object
> CObjectXml I need to:
> 
> schema_namespace::CObjectXml value(...);
> ::xml_schema::dom::auto_ptr< ::xercesc::DOMDocument > domdoc = 
>   schema_namespaces::CObjectXml_(value, contentMap);
> 
> As you can see, there is no way to templatize this since the
> serialization relies on a static function named ObjectType_ (!)

As with parsing (deserialization in your terminology), you can
serialize an object model instance directly into DOMElement.
The difference between parsing and serialization is that in
the latter case you need to know the root element name to
create the DOM document with. And that's where the element
type/map mechanism comes into play (see below).


> And with -generate-element-map I have no way to create a DOMDocument
> directly, since the serialization operators work on a DOMElement.
> Besides, it seems that whereas the ObjectType_ function correctly
> generated the top level DOMElement with name == "ObjectType", the
> latter serialization operators only serialize the CONTENT of
> ObjectType (i.e. no top element with name == "ObjectType").

Right, that's how serialization operators work. The important thing
to keep in mind is that in the general case, the same complex type
can be used in multiple root elements:

<xs:complexType name="ObjectType">
  ...
</xs:complexType>

<xs:complexType name="Root1" type="ObjectType"/>
<xs:complexType name="Root2" type="ObjectType"/>

So if all you pass to your serialization function is an instance of
ObjectType, there is no way to know which root element it should use:
Root1, Root2, something else?

And that's exactly the reason why the element type/element map
functionality was invented. An element type instance is basically
the object plus the element name. So an easy way to solve your
problem is instead of passing the complex type instance to the
serialization function, you pass the element type instance.

Take a look at the 'messaging' example, specifically, driver.cxx.
It has some code that does pretty much what you want using element
types.

Boris



More information about the xsd-users mailing list