当前位置: 技术问答>linux和unix
suse linux下libxml2 encoding="GBK" 不支持汉字的问题
来源: 互联网 发布时间:2016-03-30
本文导语: 执行编译后的程序报错: output conversion failed due to conv error Bytes: 0xCE 0xF1 0xC0 0xE0 xmlOutputBufferFlush: encoder error 当编码类型改为UTF-8时,就是xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "GBK", 1)改为xmlSaveForm...
执行编译后的程序报错:
output conversion failed due to conv error
Bytes: 0xCE 0xF1 0xC0 0xE0
xmlOutputBufferFlush: encoder error
当编码类型改为UTF-8时,就是xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "GBK", 1)改为xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1)就正确。
请问各位大虾,怎么才能支持GBK呢?
源程序如下:
output conversion failed due to conv error
Bytes: 0xCE 0xF1 0xC0 0xE0
xmlOutputBufferFlush: encoder error
当编码类型改为UTF-8时,就是xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "GBK", 1)改为xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1)就正确。
请问各位大虾,怎么才能支持GBK呢?
源程序如下:
#include
#include
#include
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
// Creates a new document, a node and set it as a root node
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "package");
xmlDocSetRootElement(doc, root_node);
xmlNewProp(root_node, BAD_CAST "version", BAD_CAST "1.0");
node = xmlNewChild(root_node, NULL, BAD_CAST "pub", NULL);
node1 = xmlNewChild(node, NULL, BAD_CAST "code", BAD_CAST "业务类型"); //此处汉字出错
//Here goes another way to create nodes.
node = xmlNewNode(NULL, BAD_CAST "req");
xmlNewProp(root_node, BAD_CAST "ansback", BAD_CAST "no");
xmlAddChild(root_node, node);
node1=xmlNewChild(node, NULL, BAD_CAST "main", NULL);
xmlNewProp(node1, BAD_CAST "fieldname", BAD_CAST "A|B|C|D");
node=xmlNewChild(node1, NULL, BAD_CAST "value", BAD_CAST "a1|b1|c1|d1");
node=xmlNewChild(node1, NULL, BAD_CAST "value", BAD_CAST "a2|b2|c2|d2");
//Dumping document to stdio or file
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "GBK", 1);
/*free the document */
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return(0);
}
|
[code=BatchFile]joey@shnr058:~/info/aaa> man iconv
Reformatting iconv(3), please wait...
ICONV(3) Linux Programmer's Manual ICONV(3)
NAME
iconv - perform character set conversion
SYNOPSIS
#include
size_t iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft);
DESCRIPTION
The argument cd must be a conversion descriptor created using the function
iconv_open().
The main case is when inbuf is not NULL and *inbuf is not NULL. In this
case, the iconv() function converts the multibyte sequence starting at
*inbuf to a multibyte sequence starting at *outbuf. At most *inbytesleft
bytes, starting at *inbuf, will be read. At most *outbytesleft bytes,
starting at *outbuf, will be written.
The iconv() function converts one multibyte character at a time, and for
each character conversion it increments *inbuf and decrements *inbytesleft
by the number of converted input bytes, it increments *outbuf and decre-
ments *outbytesleft by the number of converted output bytes, and it updates
the conversion state contained in cd. The conversion can stop for four
reasons:
1. An invalid multibyte sequence is encountered in the input. In this case
it sets errno to EILSEQ and returns (size_t)(-1). *inbuf is left pointing
to the beginning of the invalid multibyte sequence.
2. The input byte sequence has been entirely converted, i.e. *inbytesleft
has gone down to 0. In this case iconv() returns the number of non-
reversible conversions performed during this call.
3. An incomplete multibyte sequence is encountered in the input, and the
input byte sequence terminates after it. In this case it sets errno to EIN-
VAL and returns (size_t)(-1). *inbuf is left pointing to the beginning of
the incomplete multibyte sequence.
4. The output buffer has no more room for the next converted character. In
this case it sets errno to E2BIG and returns (size_t)(-1).
A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is not
NULL and *outbuf is not NULL. In this case, the iconv() function attempts
to set cd's conversion state to the initial state and store a corresponding
shift sequence at *outbuf. At most *outbytesleft bytes, starting at *out-
buf, will be written. If the output buffer has no more room for this reset
sequence, it sets errno to E2BIG and returns (size_t)(-1). Otherwise it
increments *outbuf and decrements *outbytesleft by the number of bytes
written.
A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is NULL or
*outbuf is NULL. In this case, the iconv() function sets cd's conversion
state to the initial state.
RETURN VALUE
The iconv() function returns the number of characters converted in a non-
reversible way during this call; reversible conversions are not counted.
In case of error, it sets errno and returns (size_t)(-1).
ERRORS
The following errors can occur, among others:
E2BIG There is not sufficient room at *outbuf.
EILSEQ An invalid multibyte sequence has been encountered in the input.
EINVAL An incomplete multibyte sequence has been encountered in the input.
CONFORMING TO
POSIX.1-2001.
SEE ALSO
iconv_close(3), iconv_open(3)
GNU 2001-11-15 ICONV(3)
joey@shnr058:~/info/aaa>[/code]
所以,按照iconv_open,iconv,iconv_close次序调用就行了。
随后附上另两个函数的man。(你man 3 iconv一下看看)
[code=BatchFile]joey@shnr058:~/info/aaa> man iconv_open
Reformatting iconv_open(3), please wait...
ICONV_OPEN(3) Linux Programmer's Manual ICONV_OPEN(3)
NAME
iconv_open - allocate descriptor for character set conversion
SYNOPSIS
#include
iconv_t iconv_open(const char *tocode, const char *fromcode);
DESCRIPTION
The iconv_open() function allocates a conversion descriptor suitable for
converting byte sequences from character encoding fromcode to character
encoding tocode.
The values permitted for fromcode and tocode and the supported combinations
are system dependent. For the GNU C library, the permitted values are
listed by the iconv --list command, and all combinations of the listed val-
ues are supported.
The resulting conversion descriptor can be used with iconv() any number of
times. It remains valid until deallocated using iconv_close().
A conversion descriptor contains a conversion state. After creation using
iconv_open(), the state is in the initial state. Using iconv() modifies the
descriptor's conversion state. (This implies that a conversion descriptor
can not be used in multiple threads simultaneously.) To bring the state
back to the initial state, use iconv() with NULL as inbuf argument.
RETURN VALUE
The iconv_open() function returns a freshly allocated conversion descrip-
tor. In case of error, it sets errno and returns (iconv_t)(-1).
ERRORS
The following error can occur, among others:
EINVAL The conversion from fromcode to tocode is not supported by the
implementation.
CONFORMING TO
UNIX98, POSIX.1-2001.
SEE ALSO
iconv(1), iconv(3), iconv_close(3)
GNU 1999-11-27 ICONV_OPEN(3)
joey@shnr058:~/info/aaa>[/code][code=BatchFile]joey@shnr058:~/info/aaa> man iconv_close
Reformatting iconv_close(3), please wait...
ICONV_CLOSE(3) Linux Programmer's Manual ICONV_CLOSE(3)
NAME
iconv_close - deallocate descriptor for character set conversion
SYNOPSIS
#include
int iconv_close(iconv_t cd);
DESCRIPTION
The iconv_close() function deallocates a conversion descriptor cd previ-
ously allocated using iconv_open().
RETURN VALUE
When successful, the iconv_close() function returns 0. In case of error,
it sets errno and returns -1.
CONFORMING TO
UNIX98, POSIX.1-2001.
SEE ALSO
iconv(3), iconv_open(3)
GNU 1999-11-27 ICONV_CLOSE(3)
joey@shnr058:~/info/aaa>[/code]
Reformatting iconv(3), please wait...
ICONV(3) Linux Programmer's Manual ICONV(3)
NAME
iconv - perform character set conversion
SYNOPSIS
#include
size_t iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft);
DESCRIPTION
The argument cd must be a conversion descriptor created using the function
iconv_open().
The main case is when inbuf is not NULL and *inbuf is not NULL. In this
case, the iconv() function converts the multibyte sequence starting at
*inbuf to a multibyte sequence starting at *outbuf. At most *inbytesleft
bytes, starting at *inbuf, will be read. At most *outbytesleft bytes,
starting at *outbuf, will be written.
The iconv() function converts one multibyte character at a time, and for
each character conversion it increments *inbuf and decrements *inbytesleft
by the number of converted input bytes, it increments *outbuf and decre-
ments *outbytesleft by the number of converted output bytes, and it updates
the conversion state contained in cd. The conversion can stop for four
reasons:
1. An invalid multibyte sequence is encountered in the input. In this case
it sets errno to EILSEQ and returns (size_t)(-1). *inbuf is left pointing
to the beginning of the invalid multibyte sequence.
2. The input byte sequence has been entirely converted, i.e. *inbytesleft
has gone down to 0. In this case iconv() returns the number of non-
reversible conversions performed during this call.
3. An incomplete multibyte sequence is encountered in the input, and the
input byte sequence terminates after it. In this case it sets errno to EIN-
VAL and returns (size_t)(-1). *inbuf is left pointing to the beginning of
the incomplete multibyte sequence.
4. The output buffer has no more room for the next converted character. In
this case it sets errno to E2BIG and returns (size_t)(-1).
A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is not
NULL and *outbuf is not NULL. In this case, the iconv() function attempts
to set cd's conversion state to the initial state and store a corresponding
shift sequence at *outbuf. At most *outbytesleft bytes, starting at *out-
buf, will be written. If the output buffer has no more room for this reset
sequence, it sets errno to E2BIG and returns (size_t)(-1). Otherwise it
increments *outbuf and decrements *outbytesleft by the number of bytes
written.
A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is NULL or
*outbuf is NULL. In this case, the iconv() function sets cd's conversion
state to the initial state.
RETURN VALUE
The iconv() function returns the number of characters converted in a non-
reversible way during this call; reversible conversions are not counted.
In case of error, it sets errno and returns (size_t)(-1).
ERRORS
The following errors can occur, among others:
E2BIG There is not sufficient room at *outbuf.
EILSEQ An invalid multibyte sequence has been encountered in the input.
EINVAL An incomplete multibyte sequence has been encountered in the input.
CONFORMING TO
POSIX.1-2001.
SEE ALSO
iconv_close(3), iconv_open(3)
GNU 2001-11-15 ICONV(3)
joey@shnr058:~/info/aaa>[/code]
所以,按照iconv_open,iconv,iconv_close次序调用就行了。
随后附上另两个函数的man。(你man 3 iconv一下看看)
[code=BatchFile]joey@shnr058:~/info/aaa> man iconv_open
Reformatting iconv_open(3), please wait...
ICONV_OPEN(3) Linux Programmer's Manual ICONV_OPEN(3)
NAME
iconv_open - allocate descriptor for character set conversion
SYNOPSIS
#include
iconv_t iconv_open(const char *tocode, const char *fromcode);
DESCRIPTION
The iconv_open() function allocates a conversion descriptor suitable for
converting byte sequences from character encoding fromcode to character
encoding tocode.
The values permitted for fromcode and tocode and the supported combinations
are system dependent. For the GNU C library, the permitted values are
listed by the iconv --list command, and all combinations of the listed val-
ues are supported.
The resulting conversion descriptor can be used with iconv() any number of
times. It remains valid until deallocated using iconv_close().
A conversion descriptor contains a conversion state. After creation using
iconv_open(), the state is in the initial state. Using iconv() modifies the
descriptor's conversion state. (This implies that a conversion descriptor
can not be used in multiple threads simultaneously.) To bring the state
back to the initial state, use iconv() with NULL as inbuf argument.
RETURN VALUE
The iconv_open() function returns a freshly allocated conversion descrip-
tor. In case of error, it sets errno and returns (iconv_t)(-1).
ERRORS
The following error can occur, among others:
EINVAL The conversion from fromcode to tocode is not supported by the
implementation.
CONFORMING TO
UNIX98, POSIX.1-2001.
SEE ALSO
iconv(1), iconv(3), iconv_close(3)
GNU 1999-11-27 ICONV_OPEN(3)
joey@shnr058:~/info/aaa>[/code][code=BatchFile]joey@shnr058:~/info/aaa> man iconv_close
Reformatting iconv_close(3), please wait...
ICONV_CLOSE(3) Linux Programmer's Manual ICONV_CLOSE(3)
NAME
iconv_close - deallocate descriptor for character set conversion
SYNOPSIS
#include
int iconv_close(iconv_t cd);
DESCRIPTION
The iconv_close() function deallocates a conversion descriptor cd previ-
ously allocated using iconv_open().
RETURN VALUE
When successful, the iconv_close() function returns 0. In case of error,
it sets errno and returns -1.
CONFORMING TO
UNIX98, POSIX.1-2001.
SEE ALSO
iconv(3), iconv_open(3)
GNU 1999-11-27 ICONV_CLOSE(3)
joey@shnr058:~/info/aaa>[/code]
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。