问题
Xerces transcode is returning an empty string. I think it is related to locale issues, but i'm stuck.
I have this simple program:
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/dom/DOMException.hpp>
#include <string>
#include <stdio.h>
XERCES_CPP_NAMESPACE_USE
using namespace std;
int main(int argc, char* argv[])
{
string a = "Não";
try
{
XMLPlatformUtils::Initialize("pt_PT");
} catch(const XMLException& e)
{
fprintf(stdout,"ERROR INITIALIZING\n");
}
XMLCh *auxCh3 = XMLString::transcode(a.c_str());
fprintf(stdout,"### %s ###\n",XMLString::transcode(auxCh3));
XMLString::release(&auxCh3);
XMLPlatformUtils::Terminate();
return 0;
}
And the result is:
"### ###"
If i remove the ã, the result is ok:
"### No ###"
The server's locale is defined to:
[]$ locale
LANG=pt_PT
LC_CTYPE="pt_PT"
LC_NUMERIC="pt_PT"
Which is one of the available:
[]$ locale -a|grep pt_PT
pt_PT
Thanks for your help.
回答1:
When your program starts, by default it is in "C" locale. This is defined by POSIX standard. You need to call setlocale(LC_ALL,"")
(don't forget to #include <locale.h>
) , to set the locale according to system environment variables.
来源:https://stackoverflow.com/questions/25749417/xerces-xmlstringtranscode-null-empty-reply