[xsd-users] Serialization to XML

Boris Kolpackov boris at codesynthesis.com
Wed May 3 16:47:49 EDT 2006


Hi Ricky,

Ricky H Linh <Ricky_H_Linh at RAYTHEON.COM> writes:

> If you don't mind, I would like to see an example of the work around.

Ok, suppose you have the following schema:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:complexType name="Record">
    <xsd:sequence>
      <xsd:element name="data" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <!-- We need this global element so that we get serialization
       functions for Record. -->
  <xsd:element name="record" type="Record"/>

  <xsd:complexType name="Records">
    <xsd:sequence>
      <xsd:element name="record" type="Record" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="records" type="Record"/>

</xsd:schema>

Here I assume that you generate one Record at a time and you generate
a lot of them so that constructing the whole tree in-memory is not an
option. Below is a test program that generates 1K records into a file,
one at a time. As you can see, there is some wrestling with DOM in
order to get rid of the XML declaration (<?xml version="1.0"?>) when
we serialize individual records:

#include <fstream>

#include "test.hxx"

using std::endl;

int
main ()
{
  std::ofstream ofs ("out.xml");

  // We will need to create XML declaration as well as open and close
  // the root tag ourselves.
  //
  ofs << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl
      << "<records>" << endl;

  xercesc::XMLPlatformUtils::Initialize ();

  for (unsigned long i (1); i < 1000; ++i)
  {
    // Create the next record.
    //
    Record r ("data");


    using namespace xercesc;
    namespace xml = xsd::cxx::xml;

    // Create a DOM document.
    //
    xml_schema::namespace_infomap map;
    xml::dom::auto_ptr<DOMDocument> doc (
      xml::dom::serialize ("record", "", map));


    // Serialize the record to DOM.
    //
    record (*doc, r);


    // Serialize (append) DOM to file.
    //
    DOMImplementation* impl (
      DOMImplementationRegistry::getDOMImplementation (
        xml::string ("LS").c_str ()));

    xml::dom::auto_ptr<DOMWriter> writer (impl->createDOMWriter ());

    writer->setEncoding(xml::string ("UTF-8").c_str ());

    writer->setFeature (XMLUni::fgDOMWRTDiscardDefaultContent, true);
    writer->setFeature (XMLUni::fgDOMWRTFormatPrettyPrint, true);
    writer->setFeature (XMLUni::fgDOMXMLDeclaration, false);

    xml::dom::ostream_format_target oft (ofs);
    writer->writeNode (&oft, *doc);
  }

  xercesc::XMLPlatformUtils::Terminate ();

  ofs << "</records>" << endl;
}

Also note that there is no serialization to temporary in-memory
buffer, things go directly into the file. I think this technique
could be generally useful so I am planning to support it in the
next version by adding the no_xml_declaration flag. With this
flag, the code above will become:

#include <fstream>

#include "test.hxx"

using std::endl;

int
main ()
{
  std::ofstream ofs ("out.xml");

  // We will need to create XML declaration as well as open and close
  // the root tag ourselves.
  //
  ofs << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl
      << "<records>" << endl;

  for (unsigned long i (1); i < 1000; ++i)
  {
    // Create the next record.
    //
    Record r ("data");

    // Serialize (append) the record to the file.
    //
    record (ofs, r, xml_schema::no_xml_declaration);
  }

  ofs << "</records>" << endl;
}

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/20060503/163fbf8d/attachment.pgp


More information about the xsd-users mailing list