Ok, the simple XMLString::transcode does not work
for me, so I tried with a Trancoder.
Please look at this class:
class XStr3
{
private :
XMLCh* fUnicodeForm;
XMLTranscoder* tc;
public :
//
-----------------------------------------------------------------------
// Constructors and Destructor
//
-----------------------------------------------------------------------
XStr3(XMLTranscoder* ttc, const char* const
toTranscode)
{
tc = ttc;
unsigned int bytesEaten;
unsigned int srcCount = strlen(toTranscode);
unsigned int maxChars = 1024;
unsigned char *cs = new unsigned char[1024];
memset(cs, 0, 1024);
fUnicodeForm = new XMLCh[1024];
memset(fUnicodeForm, 0, 1024);
tc->transcodeFrom((const XMLByte*)
toTranscode, srcCount, fUnicodeForm, maxChars,
bytesEaten, cs);
char *pMsg =
XMLString::transcode(fUnicodeForm);
cout << "retranscoded string:" << pMsg <<
endl;
delete [] pMsg;
XMLByte dest[1024];
unsigned int charsEaten;
tc->transcodeTo(fUnicodeForm, 1024, dest,
1024, charsEaten, XMLTranscoder::UnRep_Throw);
cout << "retranscoded string, second try:" <<
(char*)dest << endl;
}
~XStr3()
{
delete [] fUnicodeForm;
}
const XMLCh* unicodeForm() const
{
return fUnicodeForm;
}
};
#define X3(ttc, str) XStr3(ttc, str).unicodeForm()
Before I build the DOMDocument I build the
Transcoder
XMLTransService::Codes ok;
XMLTranscoder* tc =
XMLPlatformUtils::fgTransService->makeNewTranscoderFor("ISO-8859-1",
ok, 1024);
Then I build the DOMDocument (see sample
CreateDomDocument) but instead of
DOMText* devByDataVal =
doc->createTextNode(X("Apache Software
Foundation"));
is use now
DOMText* devByDataVal =
doc->createTextNode(X3(tc, "�tsch"));
..........
The output is:
retranscoded string:
retranscoded string, second try:�tsch
<?xml version="1.0" encoding="ISO-8859-1"
standalone="no"
?><company><product>Xerces-C</product><category
idea="great">XML Parsing
Toolsa</category><developedBy></developedBy></company>
.............
I still do not understand:
1) Why the simple transcode method does not work?
(see retranscoded string is empty)
2) Why is <developedBy></developedBy> empty again
? I expected the "�tsch".
Any help is very appreciate,
best regards
Thomas
Tinny Ng wrote:
>
> The XStr just uses XMLString::transcode
which transcodes the string based on
> local codepage. If your char* string is encoded in a codepage different
> from your local page, you cannot use XMLString::transcode. You should
> create your own transcoder instead. For example:
>
> char* to Unicode
> -------------------------------
> 1. If your char* is in the same encoding as your system encoding, then you
> can just call XMLString::transcode to get a unicode XMLCh* string
> e.g.
> XMLCh* result_unicode = XMLString::transcode(source_local);
>
> 2. if your char* is in an encoding other than the system one, then create
> your own transcoder and use transcodeFrom (assume your transcoder supports
> such encoding):
> e.g.
> XMLTranscoder* kkk =
> XMLPlatformUtils::fgTransService->makeNewTranscoderFor("Shift-JIS",
> failReason, 16*1024);
> xxx->transcodeFrom(source_ShiftJIS, length, result_unicode, length,
> bytesEaten, (unsigned char*)charSz);
> where source_ShiftJIS is the source char* in ShiftJIS, while the
> result_unicode is the converted XMLCh* in Unicode
>
> Unicode to char*:
> ------------------------------
> 1. If you want the XMLCh* to be converted to an encoding same as the system
> encoding, then just call XMLString::transcode
> e.g.
> char* result_local = XMLString::transcode(source_unicode);
>
> 2. If you want the XMLCh* to be converted to an encoding different from the
> local one, then create your own transcoder and use transcodeTo (assume your
> transcoder supports such encoding):
> e.g.
> XMLTranscoder* xxx =
> XMLPlatformUtils::fgTransService->makeNewTranscoderFor("Big5", failReason,
> 16*1024);
> xxx->transcodeTo(source_unicode, length, result_Big5, length, charsEaten,
> XMLTranscoder::UnRep_Throw );
> where source_unicode is the source XMLCh*, while the result_Big5 is the
> converted char* in Big5
>
> If you found anything doesn't work, please supply a test case and open a
> bugzilla bug. Thanks!
>
> Tinny
>
> ----- Original Message -----
> From: "Thomas Porschberg" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, September 20, 2002 12:22 PM
> Subject: CreateDOMDocument
>
> I have a question regarding the XStr class in
> CreateDOMDocument.
>
> //
> ---------------------------------------------------------------------------
> // This is a simple class that lets us do easy
> (though not terribly efficient)
> // trancoding of char* data to XMLCh data.
> //
> ---------------------------------------------------------------------------
> class XStr
> ...
>
> Can I expect from the XMLString::transcode method
> that it translate
> umlaute like �,�,� in a correct XMLChar ?
>
> I changed the string "Apache Software Foundation"
> in the sample code
> to "�pache Software Foundation" and the result is
> that the string
> disappears in the output.
>
> from: <developedBy>Apache Software
> Foundation</developedBy>
> to : <developedBy/>
>
> I tried to use a XML88591Transcoder instance in
> conjunction with TranscodeFrom,
> but the same result.
>
> My $LANG environment variable is set to
> "de_DE@euro",
> I set it to "de_DE" and compiled the application
> new, but the same result again.
>
> What is a straightforward way to do this ?
>
> Thomas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]