找回密码
 注册
搜索
热搜: 超星 读书 找书
查看: 4749|回复: 0

[【推荐】] LibXML2不支持中文补遗

[复制链接]
发表于 2009-8-6 14:51:27 | 显示全部楼层 |阅读模式
由于对libxml2的不熟悉,发表了“LibXML2不支持中文”一文。经过不断的接触,发现LibXML2自身已经支持了中文编码.只是他的所有api 处理的数据都是UTF-8类型的,所以只要在读入和写入数据时进行相应转换即可!而且libxml2已融合了iconv,以下是代码!flags标示是读入(0)还是写入(1)!已测试通过

uint8_t *convert(uint8_t *in, char *encoding, uint8_t flags)
{
  uint8_t *out;
  int ret, size, out_size, temp;
  xmlCharEncodingHandlerPtr handler;
  size = (int) strlen( (char*)in ) + 1;
  out_size = size * 2 - 1;
  out = (uint8_t *)malloc((size_t) out_size);
  if (out) {
    handler = xmlFindCharEncodingHandler(encoding);
    if (!handler) {
      free(out);
      out = NULL;
    }
  }
  if (out) {
    temp = size - 1;
    if ( flags ) {
      ret = handler->input(out, &out_size, in, &temp);
    }else {
      ret = handler->output(out, &out_size, in, &temp);
    }
    if (ret || temp - size + 1) {
      if (ret) {
        printf(\"conversion wasn't successful.\\n\");
      } else {
        printf(\"conversion wasn't successful. converted: \");
      }
      free(out);
      out = NULL;
    } else {
      out =(uint8_t *) realloc(out, out_size + 1);
      out[out_size] = 0; /*null terminating out */
    }
  } else {
    printf(\"no mem\\n\");
  }
  return (out);
}

int main(int argc, char **argv)
{
  uint8_t *content, *out, *in;
  xmlDocPtr doc;
  xmlNodePtr rootnode;
  char *encoding = \"ISO-8859-1\";
  //char *encoding = \"utf-8\";
  if (argc <= 1) {
    printf(\"Usage: %s content\\n\", argv[0]);
    return (0);
  }
  content = (uint8_t *)argv[1];


  out = convert(content, encoding, 1);
  in = convert( out, encoding, 0 );
  doc = xmlNewDoc( (xmlChar*)\"1.0\" );
  printf( \"%s:%s\\n\", encoding, out );
  printf( \"%s:%s\\n\", encoding, in );
  rootnode = xmlNewDocNode(doc, NULL, (const xmlChar *) \"root\", out);
  xmlDocSetRootElement(doc, rootnode);
  xmlSaveFormatFileEnc(\"-\", doc, encoding, 1);
  free( out );
  free( in );
  return (1);
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|网上读书园地

GMT+8, 2024-12-22 20:40 , Processed in 0.226705 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表