免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5872 | 回复: 4
打印 上一主题 下一主题

LIBXML处理中文XML文件的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-03-06 16:42 |只看该作者 |倒序浏览
我接触XMLLIB时间不长,有一问题向各位求教,这是一个从网上下载的例子。

源码如下addKeyword.c:
#include <stdio.h>;
#include <string.h>;
#include <stdlib.h>;
#include <libxml/xmlmemory.h>;
#include <libxml/parser.h>;

void
parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {

xmlNewTextChild (cur, NULL, "keyword", keyword);
    return;
}

xmlDocPtr
parseDoc(char *docname, char *keyword) {

xmlDocPtr doc;
xmlNodePtr cur;

doc = xmlParseFile(docname);

if (doc == NULL ) {
  fprintf(stderr,"Document not parsed successfully. \n";
  return (NULL);
}

cur = xmlDocGetRootElement(doc);

if (cur == NULL) {
  fprintf(stderr,"empty document\n";
  xmlFreeDoc(doc);
  return (NULL);
}

if (xmlStrcmp(cur->;name, (const xmlChar *) "story") {
  fprintf(stderr,"document of the wrong type, root node != story";
  xmlFreeDoc(doc);
  return (NULL);
}

cur = cur->;xmlChildrenNode;
while (cur != NULL) {
  if ((!xmlStrcmp(cur->;name, (const xmlChar *)"storyinfo")){
   parseStory (doc, cur, keyword);
  }
   
cur = cur->;next;
}
return(doc);
}

int
main(int argc, char **argv) {

char *docname;
char *keyword;
xmlDocPtr doc;

if (argc <= 2) {
  printf("Usage: %s docname, keyword\n", argv[0]);
  return(0);
}

docname = argv[1];
keyword = argv[2];
doc = parseDoc (docname, keyword);
if (doc != NULL) {
  xmlSaveFormatFile (docname, doc, 0);
  xmlFreeDoc(doc);
}

return (1);
}

其所使用的英文XML样例如下example.xml(UTF-8编码):
<?xml version="1.0"?>;
<story>;
  <storyinfo>;
    <author>;John Fleck</author>;
    <datewritten>;June 2, 2002</datewritten>;
  <body>;
    <headline>;This is the headline</headline>;
    <para>;This is the body text.</para>;
  </body>;
</story>;

其所使用的含中文XML样例如下example_c.xml(UTF-8编码):
<?xml version="1.0"?>;
<story>;
  <storyinfo>;
    <author>;发射点反抗六十九赶快John Fleck</author>;
    <datewritten>;June 2, 2002士大夫士大夫</datewritten>;
    <keyword>;example 士大夫士大夫士大夫感keyword</keyword>;
  </storyinfo>;
  <body>;
    <headline>;豆腐干大概This is the headline</headline>;
    <para>;This is the 大幅度法ody text.</para>;
  </body>;
</story>;


用addKeyword运行:
addKeyword example.xml test_add_keyword
addKeyword example_c.xml test_add_keyword

结果英文xml文件运行后正常,而中文xml变成:
<?xml version="1.0"?>;
<story>;
  <storyinfo>;
   

<author>;&amp;#x53D1;&amp;#x5C04;&amp;#x70B9;&amp;#x53CD;&amp;#x6297;&amp;#x516D;&amp;#x5341;&amp;#x4E5D;&amp;#x8D76;&amp;#x5FEB;

John Fleck</author>;
    <datewritten>;June 2, 2002&amp;#x58EB;&amp;#x5927;&amp;#x592B;&amp;#x58EB;&amp;#x5927;&amp;#x592B;</datewritten>;
    <keyword>;example

&amp;#x58EB;&amp;#x5927;&amp;#x592B;&amp;#x58EB;&amp;#x5927;&amp;#x592B;&amp;#x58EB;&amp;#x5927;&amp;#x592B;&amp;#x611F;keyword</

keyword>;
  <keyword>;test_add_keyword</keyword>;</storyinfo>;
  <body>;
    <headline>;&amp;#x8C46;&amp;#x8150;&amp;#x5E72;&amp;#x5927;&amp;#x6982;This is the headline</headline>;
    <para>;This is the &amp;#x5927;&amp;#x5E45;&amp;#x5EA6;&amp;#x6CD5;ody text.</para>;
  </body>;
</story>;

不知诸位有何高见,望不吝赐教。本人不胜感激!

论坛徽章:
0
2 [报告]
发表于 2003-03-06 18:16 |只看该作者

LIBXML处理中文XML文件的问题

那可能是你的XML库不支持中文


你可以到这个库的开发者去问问怎样才能输入中文

论坛徽章:
0
3 [报告]
发表于 2003-09-19 17:25 |只看该作者

LIBXML处理中文XML文件的问题

加上
<?xml version="1.0" encoding = "ISO-8859-2"?>;
就可以了

论坛徽章:
0
4 [报告]
发表于 2003-09-22 18:34 |只看该作者

LIBXML处理中文XML文件的问题

各位大侠,我在sco下测试,英文可以。中文时出现
iconv_open cannot open conversion file /usr/lib/nls/conv/ISO-8859-2_UTF-8
iconv_open cannot open conversion file /usr/lib/nls/conv/UTF-8_ISO-8859-2
请问如何解决?

论坛徽章:
0
5 [报告]
发表于 2003-09-23 10:11 |只看该作者

LIBXML处理中文XML文件的问题

libxml2-2.5.4 支持的encoding,当个版本新一点的

    XML_CHAR_ENCODING_ERROR=   -1, /* No char encoding detected */
    XML_CHAR_ENCODING_NONE=        0, /* No char encoding detected */
    XML_CHAR_ENCODING_UTF8=        1, /* UTF-8 */
    XML_CHAR_ENCODING_UTF16LE=        2, /* UTF-16 little endian */
    XML_CHAR_ENCODING_UTF16BE=        3, /* UTF-16 big endian */
    XML_CHAR_ENCODING_UCS4LE=        4, /* UCS-4 little endian */
    XML_CHAR_ENCODING_UCS4BE=        5, /* UCS-4 big endian */
    XML_CHAR_ENCODING_EBCDIC=        6, /* EBCDIC uh! */
    XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
    XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
    XML_CHAR_ENCODING_UCS2=        9, /* UCS-2 */
    XML_CHAR_ENCODING_8859_1=        10,/* ISO-8859-1 ISO Latin 1 */
    XML_CHAR_ENCODING_8859_2=        11,/* ISO-8859-2 ISO Latin 2 */
    XML_CHAR_ENCODING_8859_3=        12,/* ISO-8859-3 */
    XML_CHAR_ENCODING_8859_4=        13,/* ISO-8859-4 */
    XML_CHAR_ENCODING_8859_5=        14,/* ISO-8859-5 */
    XML_CHAR_ENCODING_8859_6=        15,/* ISO-8859-6 */
    XML_CHAR_ENCODING_8859_7=        16,/* ISO-8859-7 */
    XML_CHAR_ENCODING_8859_8=        17,/* ISO-8859-8 */
    XML_CHAR_ENCODING_8859_9=        18,/* ISO-8859-9 */
    XML_CHAR_ENCODING_2022_JP=  19,/* ISO-2022-JP */
    XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
    XML_CHAR_ENCODING_EUC_JP=   21,/* EUC-JP */
    XML_CHAR_ENCODING_ASCII=    22 /* pure ASCII */
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP