You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Ben Martin <mo...@users.sourceforge.net> on 2006/10/17 01:43:59 UTC

Transcode a blob to XMLCh*

Hi,
  Given arbitrary data in a std::string what is the recommended way to
turn that into a CDATA node when building a DOM?

string mystr = ...;
XMLCh* xch = XMLString::transcode( mystr.c_str() );
DOMElement* el = ...;
DOMCDATASection* childVal = dom->createCDATASection( xch );
el->appendChild( childVal );

It seems to be difficult to preserve arbitrary 8 bit data from a
std::string into a CDATA definition.

Thanks for any advice.

Re: Transcode a blob to XMLCh*

Posted by David Bertoni <db...@apache.org>.
Ben Martin wrote:
> Hi,
>   Given arbitrary data in a std::string what is the recommended way to
> turn that into a CDATA node when building a DOM?
> 

What do you mean by arbitrary data?  If you mean characters that are not 
allowed in XML documents, then you must encode them somehow.  You might 
consider using base64, for example.

> string mystr = ...;
> XMLCh* xch = XMLString::transcode( mystr.c_str() );

Using XMLString::transcode() to convert "arbitrary" data to UTF-16 presumes 
the local code page is a single byte encoding that contains a character at 
every code point, and that each of those characters maps to a Unicode code 
point.

> DOMElement* el = ...;
> DOMCDATASection* childVal = dom->createCDATASection( xch );
> el->appendChild( childVal );
> 
> It seems to be difficult to preserve arbitrary 8 bit data from a
> std::string into a CDATA definition.

I think you really want to use something like base64.  That way, you don't 
even need to use a CDATA section.

Dave