[xsd-users] Serialization of a XML to a UNICODE file name

Boris Kolpackov boris at codesynthesis.com
Fri Nov 5 11:09:08 EDT 2010


Hi Constantin,

Constantin Iacobescu <sir.costy at gmail.com> writes:

> But I have just get in trouble on trying to serialize the xml into a unicode
> named file, because the generated code is expecting a ostream and I need a 
> wostream...

The generated code uses std::o/istream for XML input and output because,
in the general case, the stream is binary, not text and the underlying
i/o unit is one byte, not one character.

The only way to overcome this with std streams would be to convert the
wide character file name to the multi-byte char string and use that
with std::o/istream. However, depending on the locale, there can be
loss of data.

A better way to handle this would be to use Xerces-C++ file streams
which always use UTF-16 file names. Here is some sample code (based 
on the library example):


#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>

using namespace xercesc;

int
main ()
{
  XMLPlatformUtils::Initialize ();

  // Parse.
  //
  LocalFileInputSource is (L"input.xml");
  std::auto_ptr<catalog> c (catalog_ (is));

  ...

  // Serialize.
  //
  LocalFileFormatTarget os (L"output.xml");
  catalog_ (os, *c, map);

  XMLPlatformUtils::Teminate ();
}

Boris



More information about the xsd-users mailing list