[xsd-users] XML Object to String/Text

Boris Kolpackov boris at codesynthesis.com
Wed Feb 21 11:41:39 EST 2007


Hi Paddy,

Paddy Vishnubhatt <Paddy.Vishnubhatt at 3leafnetworks.com> writes:

> In order to use route 1, is it a must to create a document object
> in the XSD definition like how you did it in library example?

Yes, serialization functions (e.g., catalog_ in the example I gave)
are generated only for global elements (since they are valid document
roots).

If you want to serialize individual objects that correspond to
types defined in XML Schema then you will need to use DOM. Here
is how you can do this:

1. Create DOM document using the create function from Q 3.1 in FAQ[1]:

    xml::dom::auto_ptr<DOMDocument> doc (
      create ("example",
              "http://www.example.com/xmlns/example",
              "e"));

    DOMElement* root (doc->getDocumentElement ());


Use your element name, namespace and prefix. Also don't forget to
call XMLPlatformUtils::Initialize ().


2. Serialize your object to the document's root element (remember to
   compile your schemas with --generate-serialization):

   my_obj& obj = ...

   DOMElement& root (*doc->getDocumentElement ());

   root << obj;


3. Serialize the DOM document to string using code from Q 3.2:

   std::ostringstream ostr;
   serialize (ostr, *doc);

   std::string str = ostr.str ();


As you can see, you will need to create a DOM document with the
root element name of your choice. Since the object does not have
this information (and an XML Schema type can be used as a type
for several elements), there is no way to provide a function like
to_xml_as_string. I am not sure how Java frameworks work around
this.


[1] http://wiki.codesynthesis.com/Tree/FAQ


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/20070221/55c1edfc/attachment.pgp


More information about the xsd-users mailing list