免费注册 查看新帖 |

Chinaunix

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

[XML] C解析XML???急救!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-03 15:18 |只看该作者 |倒序浏览
哪位大哥用C解析过XML格式的数据,请给小弟指点一下,多谢了!!
最好给个例子!!

[ 本帖最后由 HonestQiao 于 2006-6-3 15:20 编辑 ]

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
2 [报告]
发表于 2006-06-03 15:21 |只看该作者
xml.apache.org

论坛徽章:
0
3 [报告]
发表于 2006-06-05 10:48 |只看该作者
libxml2

[ 本帖最后由 ilcj 于 2006-6-5 10:53 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-06-05 18:13 |只看该作者

请说的详细一点!

#include <stdio.h>;
#include <string.h>;
#include <stdlib.h>;
#include <libxml/xmlmemory.h>;
#include <libxml/parser.h>;

void
parseStory (xmlDocPtr doc, xmlNodePtr cur) {

        xmlChar *key;
        cur = cur->;xmlChildrenNode;
        while (cur != NULL) {
            if ((!xmlStrcmp(cur->;name, (const xmlChar *)"keyword"))) {
                    key = xmlNodeListGetString(doc, cur->;xmlChildrenNode, 1);
                    printf("keyword: %s\n", key);
                    xmlFree(key);
            }
        cur = cur->;next;
        }
    return;
}

static void
parseDoc(char *docname) {

        xmlDocPtr doc;
        xmlNodePtr cur;

        doc = xmlParseFile(docname);
        
        if (doc == NULL ) {
                fprintf(stderr,"Document not parsed successfully. \n");
                return;
        }
        
        cur = xmlDocGetRootElement(doc);
        
        if (cur == NULL) {
                fprintf(stderr,"empty document\n");
                xmlFreeDoc(doc);
                return;
        }
        
        if (xmlStrcmp(cur->;name, (const xmlChar *) "story")) {
                fprintf(stderr,"document of the wrong type, root node != story");
                xmlFreeDoc(doc);
                return;
        }
        
        cur = cur->;xmlChildrenNode;
        while (cur != NULL) {
                if ((!xmlStrcmp(cur->;name, (const xmlChar *)"storyinfo"))){
                        parseStory (doc, cur);
                }
                 
        cur = cur->;next;
        }
        
        xmlFreeDoc(doc);
        return;
}

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

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

        docname = argv[1];
        parseDoc (docname);

        return (1);
}
我这里有个例子,但是编译总是不成功,出现好多错误!错误如下:请大家帮忙找一下:
parse.c:4:30: libxml/xmlmemory.h: No such file or directory
parse.c:5:27: libxml/parser.h: No such file or directory
parse.c:8: parse error before "doc"
parse.c: In function `parseStory':
parse.c:10: `xmlChar' undeclared (first use in this function)
parse.c:10: (Each undeclared identifier is reported only once
parse.c:10: for each function it appears in.)
parse.c:10: `key' undeclared (first use in this function)
parse.c:11: `cur' undeclared (first use in this function)
parse.c:13: parse error before "xmlChar"
parse.c: At top level:
parse.c:20: parse error before "return"
parse.c: In function `parseDoc':
parse.c:26: `xmlDocPtr' undeclared (first use in this function)
parse.c:26: parse error before "doc"
parse.c:27: `xmlNodePtr' undeclared (first use in this function)
parse.c:29: `doc' undeclared (first use in this function)
parse.c:36: `cur' undeclared (first use in this function)
parse.c:44: parse error before "xmlChar"
parse.c: At top level:
parse.c:50: `cur' used prior to declaration
parse.c:50: invalid type argument of `->;'
parse.c:50: warning: data definition has no type or storage class
parse.c:51: parse error before "while"
parse.c:56: redefinition of `cur'
parse.c:50: `cur' previously defined here
parse.c:56: invalid type argument of `->;'
parse.c:56: warning: data definition has no type or storage class
parse.c:57: parse error before '}' token
parse.c:59: warning: parameter names (without types) in function declaration
parse.c:59: warning: data definition has no type or storage class
parse.c:60: parse error before "return"
parse.c:77:2: warning: no newline at end of file

论坛徽章:
0
5 [报告]
发表于 2006-06-10 18:40 |只看该作者
另外 "story","storyinfo","keyword"分别是XML文件中的节点元素吗?
就相当于
- <story>
- <storyinfo>
  <author>John Fleck</author>
  <datewritten>June 2, 2002</datewritten>
  <keyword>我来也 example keyword</keyword>
  <书目>C++</书目>
  <测试>test</测试>
  </storyinfo>
- <body>
  <headline>This is the headline</headline>
  <para>This is the body text.</para>
  </body>
  </story>
中的story,storyinfo,keywords吗?
如果是为什么执行./x l.xml   一点反映都没有呢?(x为可执行程序名称,l.xml为xml文件)
编译命令是:gcc -o x x.c -I/usr/include/libxml2 -L/usr/lib -lxml2 -lz -lm
编译后仍然有上面的警告信息?

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
6 [报告]
发表于 2006-06-10 23:52 |只看该作者
parse.c:4:30: libxml/xmlmemory.h: No such file or directory
parse.c:5:27: libxml/parser.h: No such file or directory


提示很清楚了

论坛徽章:
0
7 [报告]
发表于 2006-06-11 12:06 |只看该作者
编译后,错误已经没有了大哥
只有警告信息啊

论坛徽章:
0
8 [报告]
发表于 2006-06-14 18:35 |只看该作者
请大家帮忙啊
求救版主

论坛徽章:
0
9 [报告]
发表于 2006-06-15 10:17 |只看该作者
waring 的内容是什么

论坛徽章:
0
10 [报告]
发表于 2006-07-04 11:47 |只看该作者
采用数据结构处理,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP