Hi Holger,
Close, but not quite! ;-)
The Xerces DOM transcode member function returns a pointer to a memory
buffer (char*), but XalanDOMString returns a copy of a std::vector which
contains the transcoded characters. The reason for this is illustrated by
the problem with your sample, if used with the Xerces DOM -- you've
introduced a memory leak into your application. The proper way to do this
is Xerces is to get a pointer to the result, then delete it after you've
finished. Using the old, deprecated Xerces DOM:
void
foo(
DOM_Node node,
std::string& nodeName)
{
DOMString nodeName = node.getNodeName();
char* const temp = node.getNodeName().transcode();
nodeName = temp;
delete [] temp;
}
The same applies to the Xerces function XMLString::transcode().
My approach in Xalan was different, because I don't like transfer of
ownership that requires users to read documentation -- I've just seen way
to many leaks introduced that way. So, in XalanDOMString::transcode(), you
get back a std::vector which will manage the memory. You can get at a
null-terminated const char* by doing any of the following:
CharVectorType nodeName = node.getNodeName().transcode();
1. &nodeName[0];
2. &*nodeName.begin();
3. c_str(nodeName);
If you don't like the inefficiency of multiple copies of the data (because
a std::vector is returned by value), you can use the
TranscodeToLocalCodePage() function directly. Also, I've just added a new
transcode() overload:
void
transcode(CharVectorType& theResult);
Hope that helps...
Dave
Holger Floerke
<floerke@doctron To: [EMAIL PROTECTED]
ic.de> cc: (bcc: David N
Bertoni/Cambridge/IBM)
Subject: Re: XalanDOMString to
std::string
01/10/2003 11:55
PM
Please respond
to xalan-dev
Hi Steve,
>I'm probably missing something very simple but I can't seem to get a
>XalanDOMString converted to a std::string. I'm using Xalan C++ in a
>Windows environment. My XML uses UTF-8 encoding.
You have to use the member function "transcode" to transcode the string to
the local codepage. It doesn't matter, which kind of encoding your XML has.
Do you want to transcode the string to the local codepage, or you think of
storing the UTF-8 characters in the std::string?
I'm more familiar with the XercesDOM, but if you think of transcoding to
the local codepage, try this code fragment (unverified):
string oLocalString( &(oMyXalanDOMString.transcode()) )
Hope this works and helps,
HolgeR
--
holger floerke d o c t r o n i c
email [EMAIL PROTECTED] information publishing + retrieval
phone +49 2222 9292 90 http://www.doctronic.de