Re: [xml] libxml + unicode



Hi. You can use 'iconv' for recode a string into another encoding. For that you must use 'iconv_open' and 'iconv_close' for open an close a handler for recode (use man iconv_open and man iconv_close for help). Later, for recode the string you must use function 'iconv' (man 3 iconv for help), but, for better help, consult internet to view examples because use directly that funcion is incorrect and won't obtain the results what you want. I put an example here:

string Convert::convert(const string &text) const
{
        iconv(_con, NULL, NULL, NULL, NULL);                            // Necessary at beggining

        size_t enterSize = text.size();

        size_t exitSize = enterSize * 2;                                // The maximum size will be the double than the sou
        char *ex = (char*)malloc(exitSize * sizeof(char) + 1);          // Allocating memory
        char *exit = ex;                                                // Aux
        #ifdef __WIN32__
        const char *enter = (const char*)text.c_str();
        #else
        char *enter = (char*)text.c_str();
        #endif

        memset(exit, 0, exitSize);                                      // Setting all bytes the value 0

        size_t res = iconv(_con, &enter, &enterSize, &ex, &exitSize);   // Converting
        if (res == (size_t)(-1))                                        // If the function 'iconv' can't convert --> error
                throw(ConvertException::CONVERT);
        string ret(exit, ex - exit);
        free(exit);                                                     // Freeing memory
        return ret;
}

(Sorry, but I use C++, but the utilization is the same).

Where '_con' is the handler used with 'iconv_open'. That code works on Windows and Linux.

I hope that instructions serve you.

Regards: Marcel.

Mark Wyszomierski wrote:
Hi,
 
I'm trying to print some characters that were saved in a unicode format (some special french accent characters). I was hoping the following would work but I just get strange characters printed:
    xmlChar *key = xmlNodeListGetString(doc, curNode->xmlChildrenNode, 1);
    wprintf(L"Node text is: [%s]\n", key);
    xmlFree(key);

My xml document looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
         <test>woo: Ç Ç Ç</test>

What do I have to do to get those special characters to be printed correctly? In fact, now when I save my XML document as type UTF-8, even the ascii characters don't print correctly. (printing ASCI chars when saved in ASCI format works fine).

Thanks,

Mark


_______________________________________________ xml mailing list, project page http://xmlsoft.org/ xml gnome org http://mail.gnome.org/mailman/listinfo/xml

Attachment: marcel.vcf
Description: Vcard



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]