As I understand it, yes.
In Obyx we do something like this:

typedef std::basic_string<XMLCh> u_str;

//u_str to std::string.
void transcode(const u_str& source, std::string& result,const std::string 
encoding) {
        result.clear();
        if (!source.empty()) {
                char* buff = NULL;
                const XMLCh* src=(const XMLCh*)(source.c_str());
                buff = 
(char*)(TranscodeToStr(src,source.size(),encoding.c_str()).adopt());
                if (buff != NULL) { 
                        result = buff; 
                        XMLString::release(&buff);
                }
        }
}

//std::string to u_str
void transcode(const std::string& source,u_str& result,const std::string 
encoding) {
        result.clear();
        if (!source.empty()) {
                XMLCh* buf = NULL;
                const XMLByte* src=(const XMLByte*)(source.c_str());
                
buf=TranscodeFromStr(src,source.size(),encoding.c_str()).adopt();       
                if (buf != NULL) { 
                        result = buf; 
                        XMLString::release(&buf);
                }
        }
}

Of course, it is best to do what you can to reduce the amount of transcoding 
needed, especially when declaring constants.
We use a shorthand (which is frowned upon by the developers so use at your own 
risk, but it works fine for me on several platforms with a vanilla xercesc.):

#define UCS2(x) (const XMLCh*)(x)

Then any XMLCh constants are defined as following:

const u_str  xml_ns = UCS2(L"http://www.w3.org/XML/1998/namespace";);    //no 
need to transcode.

I then access it using eg xml_ns.c_str()

I think the developers prefer something like:

const XMLCh xml_ns[]= 
{'h','t','t','p',':','/','/','w','w','w','.','w','3','.','o','r','g','/','X','M','L','/','1','9','9','8','/','n','a','m','e','s','p','a','c','e',chNull};
 // http://www.w3.org/XML/1998/namespace

Just my 2ยข


On 30 Jun 2010, at 12:55, <[email protected]> wrote:

> Hi,
> 
> I'm working with a software, which makes wide use of constructs like this:
> myDocument->createElement(XMLString::transcode("MyELement"));
> 
> I think this should result in a memoryhole, at least with non xercesclasses I 
> am sure.
> And as the Parameter to createElement is const it should be here too.
> So, am I right, that I need to create a local variable pointing to the string 
> returned from transcode, to free with XMLString::release after the call of 
> createElement?
> 
> Regards,
> 
> Jens Weller



Reply via email to