Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2312255
  • 博文数量: 473
  • 博客积分: 12252
  • 博客等级: 上将
  • 技术积分: 4307
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-12 10:02
文章分类

全部博文(473)

文章存档

2012年(8)

2011年(63)

2010年(73)

2009年(231)

2008年(98)

分类:

2010-06-09 17:32:50

来自官网例子(),稍微改了下:
/*******************************************
* compile: gcc -I/usr/include/libxml2/ -lxml2 test_xml.c
 * usage: tree2 filename_or_URL
 *
*******************************************/
#include
#include
#include
 
static void print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;
 
    for (cur_node = a_node; cur_node; cur_node = cur_node->next)
    {
        if (cur_node->type == XML_ELEMENT_NODE)
        {
            printf("node type: Element, name: %s\n", cur_node->name);
        }
        else if(cur_node->type == XML_TEXT_NODE)
        {
            printf("text: %s\n",xmlNodeGetContent(cur_node));//获取节点值
        }
        print_element_names(cur_node->children);
    }
}
 
/**
 * Simple example to parse a file called "file.xml",
 * walk down the DOM, and print the name of the
 * xml elements nodes.
 */
int main(int argc, char **argv)
{
    xmlDoc *doc = NULL;
    xmlNode *root_element = NULL;
 
    if (argc != 2)
        return(1);
 
    //LIBXML_TEST_VERSION
 
    /*parse the file and get the DOM */
    doc = xmlReadFile(argv[1], NULL, 0);
 
    if (doc == NULL) {
        printf("error: could not parse file %s\n", argv[1]);
    }
 
    /*Get the root element node */
    root_element = xmlDocGetRootElement(doc);
 
    print_element_names(root_element);
 
    /*free the document */
    xmlFreeDoc(doc);
 
    //xmlCleanupParser();
 
    return 0;
}
阅读(6163) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:windows统计一个目录下的文件大小和数量

给主人留下些什么吧!~~