List Info

Thread: libxml + unicode




libxml + unicode
user name
2006-07-26 07:40:53
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:
mail.gmail.com" type="cite">
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" ?>
&nbsp; &nbsp; &nbsp;   ; <test&gt;woo: Ç Ç Ç&lt;/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/ gnome.org">xmlgnome.org http://mail.gnome.org/mailman/listinfo/xml
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )