原文地址http://stackoverflow.com/questions/9826518/purpose-of-xmlstringtranscode
I don't seem to understand the purpose of XMLString::transcode(XMLCh*)
andXMLString::transcode(char*)
, because obviously I don't understand the difference betweenXMLCh*
and char*
. Can someone please make things clearer for me ?
Xerces encodes information as UTF-16 internally. The UTF-16 data is stored using the XMLCh
datatype.
'C-style' strings use char
which is in the local code page (probably UTF-8, but it depends on the platform and settings) You use transcode
to convert between the two.
For instance if you want to feed some data from Xerces to another library and that library expects text in the local code page, you need to transcode
it. Also, if you have char
data and want to feed it to Xerces, you need to transcode
it to XMLCh
, because that is what Xerces understands.
For example:
// to local code pageDOMNode*node =...;char* temp =XMLString::transcode(node->getNodeValue());
std::string value(temp);XMLString::release(&temp);// from local code pageDOMElement*element =...;XMLCh*tag =XMLString::transcode("test");DOMNodeList*list= element->getElementsByTagName(tag);XMLString::release(&tag);
Do not forget to release the string! Better is to write some wrapper round it but there are examplesavailable on the internet (just search for a class named XercesString
).