"Brendan Reville" <[EMAIL PROTECTED]> writes:

> I have a .xml file encoded as utf-8, and I want to load some
> attribute values as utf-8 strings inside my own code.
> 
> given that AttributeList::getName()) returns an XMLCh*, which I take
> to be Unicode characters, how can I then convert this back into a
> utf-8 string?

Here's a bit of C-ish typemap code that does it. Note that $source is
the XMLCh* input string, and UTF8_TRANSCODER is a global variable set
to a pre-allocated transcoder for UTF-8.

  unsigned int charsEaten = 0;
  int length  = XMLString::stringLen($source);      // string length
  XMLByte* res = new XMLByte[length * UTF8_MAXLEN];          // output string
  unsigned int total_chars =
    UTF8_TRANSCODER->transcodeTo((const XMLCh*) $source, 
                                   (unsigned int) length,
                                   (XMLByte*) res,
                                   (unsigned int) length*UTF8_MAXLEN,
                                   charsEaten,
                                   XMLTranscoder::UnRep_Throw
                                   );
  res[total_chars] = '\0';

Here's the code that allocates the transcoder:

static XMLCh* UTF8_ENCODING = NULL; 
static XMLTranscoder* UTF8_TRANSCODER  = NULL;
    XMLTransService::Codes failReason;
    XMLPlatformUtils::Initialize(); // first we must create the transservice
    UTF8_ENCODING = XMLString::transcode("UTF-8");
    UTF8_TRANSCODER =
      XMLPlatformUtils::fgTransService->makeNewTranscoderFor(UTF8_ENCODING,
                                                             failReason,
                                                             1024);
    if (! UTF8_TRANSCODER) {
        croak("ERROR: XML::Xerces: INIT: Could not create UTF-8 transcoder");
    }

HTH,
jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to