[xsd-users] serialization functions as members ?

Boris Kolpackov boris at codesynthesis.com
Tue Jan 9 07:15:57 EST 2007


Hi Bradley,

Bradley Beddoes <beddoes at intient.com> writes:

> Is there any way to have serialization functions generated as members of
> the actual class of your XSD type?.
>
> For example:
>
> <xsd:element name="Name" type="NameType"/>
>
> This would currently generate the serialisation functions as standalone
> function calls for example like this:
>
> void
> Name (std::ostream&,
>       const NameType&,
>       const xml_schema::namespace_infomap&,
>       const string& encoding = "UTF-8",
>       xml_schema::flags = 0);
>
> I am trying to write a generic marshaller library (integrated with
> xerces and xml security) and generating the serialization stuff this way
> make it impossible for me to do this. Instead is it possible to have for
> example:
>
> class NameType
> {
> 	public:
> 		void serialize (std::ostream&,
>       const xml_schema::namespace_infomap&,
>       const string& encoding = "UTF-8",
>       xml_schema::flags = 0);
>
> 	....
> }
>
> ??
>
> This would allow me to define a generic function such as:
>
> MyMarshaller::Marshall(T xmlObj)
>
> Which would be able to serialize any object it was instantiated to serve
> at runtime which would really help enormously.

The reason why we generate serialization functions for global elements
instead of every type is because only such global elements are valid
document roots (as per XML Schema). Also generating a set of
serialization functions (about 10) for each type would result in quite
a lot of extra code that is not used.

The way to do what you want is to use serialization operators that
are generated for each type in the form:

void operator<< (DOMElement&, Type const&);

You can create a DOM document in your Marshall() function, serialize
 the instance to its root element, and then serialize DOM to XML.
You probably know how to do all this but, just in case, the following
FAQ section has some code:

http://wiki.codesynthesis.com/Tree/FAQ#Serialization


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20070109/efeabab8/attachment.pgp


More information about the xsd-users mailing list