[xsd-users] A Question about Unicode Character

Boris Kolpackov boris at codesynthesis.com
Wed Apr 16 04:00:29 EDT 2008


Hi,

lvkun <lvkun2006 at gmail.com> writes:

> I need to convert the string to CString(MFC). So I tried to convert
> it to wstring first.

Ok, here is what you can do (this method is Windows-specific). You
will need to first serialize the object model to DOMDocument. Note
also that you will need to initialize and terminate the Xerces-C++
runtime. Using the example from your previous email this can look
like this:

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/PlatformUtils.hpp>

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

  {
    xml_schema::dom::auto_ptr<xercesc::DOMDocument> doc = Param_(
      param, map);
  }

  xercesc::XMLPlatformUtils::Terminate ();
}


Once you get the DOM document, you can use the Xerces-C++ DOMWriter's
writeToString function to serialize this document to XMLCh string
which, on Windows, is the same as wchar_t. There is a piece of code
in Q 3.2 in the C++/Tree Mapping FAQ that shows how to do it except
that it serializes to std::ostream instead of XMLCh string:

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

To make it write to wstring instead of ostream you can replace this
part:

  // Adapt ostream to format target and serialize.
  //
  xml::dom::ostream_format_target oft (os);

  writer->writeNode (&oft, doc);

  eh.throw_if_failed<tree::parsing<char> > ();


With the following code:

  XMLCh* str = writer->writeToString (doc);

  eh.throw_if_failed<tree::parsing<char> > ();

  std::wstring wstr (str);

  XMLString::release (&str);

Boris




More information about the xsd-users mailing list