使用Expat XML解析器的例子

2011-05-30 19:32:32|  分类: 默认分类 |字号 订阅
工作原理: 
1 . Initialize the XML parser with the xml_parser_create() function 
通过xml_parser_create()函数安装XML解析器; 
2.  Create functions to use with the different event handlers 
使用不同的事件处理其建立函数; 
3.  Add the xml_set_element_handler() function to specify which function will be executed when the parser encounters the opening and closing tags 
通过添加xml_set_element_handler()函数来指定需要执行的函数(当解析器遇到开始或结束标签时) 
4. Add the xml_set_character_data_handler() function to specify which function will execute when the parser encounters character data 
通过添加xml_set_element_handler()函数来指定需要执行的函数(当解析器遇到字符时) 
5. Parse the file "test.xml" with the xml_parse() function 
通过xml_parse()函数来解析“test.xml”文件 
6. In case of an error, add  xml_error_string() function to convert an XML error to a textual description 
当出现错误情况时,通过添加xml_error_string()函数将一个XML的错误转换成一个文本描述 
7 Call the xml_parser_free() function to release the memory allocated with the xml_parser_create() function 
通过请求xml_parser_free()函数来释放xml_parser_create()函数分配的内存


 


地址:http://blog.csdn.net/caimouse/archive/2008/05/18/2455644.aspx
要了解第二人生里使用expat XML解析器之前,先来仔细地分析一下怎么样使用expat库的小例子,看看具体调用了那些接口函数,是否会很复杂的呢?‘它的例子程序如下:
#001 /*****************************************************************
#002   * outline.c
#003   *
#004   * Copyright 1999, Clark Cooper
#005   * All rights reserved.
#006   *
#007   * This program is free software; you can redistribute it and/or
#008   * modify it under the same terms as Perl.
#009   *
#010   * Read an XML document from standard input and print an element
#011   * outline on standard output.
#012   */
#013 
#014 
 
下面包括输出文件和库文件头。
#015 #include <stdio.h>
#016 #include "xmlparse.h"
#017 
 
定义缓冲区的大小。
#018 #define BUFFSIZE   8192
#019 
 
创建一个缓冲区。
#020 char Buff[BUFFSIZE];
#021 
#022 int Depth;
#023 
 
下面定义一个XML元素开始处理的函数。
#024 void
#025 start(void *data, const char *el, const char **attr) {
#026    int i;
#027 
#028    for (i = 0; i < Depth; i++)
#029      printf(" ");
#030 
#031    printf("%s", el);
#032 
#033    for (i = 0; attr[i]; i += 2) {
#034      printf(" %s='%s'", attr[i], attr[i + 1]);
#035    }
#036 
#037    printf("\n");
#038    Depth++;
#039 } /* End of start handler */
#040 
 
下面定义一个XML元素结束调用的函数。
#041 void
#042 end(void *data, const char *el) {
#043    Depth--;
#044 } /* End of end handler */
#045 
 
程序入口点。
#046 void
#047 main(int argc, char **argv) {
 
创建一个XML分析器。
#048    XML_Parser p = XML_ParserCreate(NULL);
 
下面判断是否创建XML分析器失败。
#049    if (! p) {
#050      fprintf(stderr, "Couldn't allocate memory for parser\n");
#051      exit(-1);
#052    }
#053 
 
下面设置每个XML元素出现和结束的处理函数。这里设置start为元素开始处理函数,end元素结束处理函数。
#054    XML_SetElementHandler(p, start, end);
#055 
 
循环分析所有XML文件。
#056    for (;;) {
#057      int done;
#058      int len;
#059 
 
调用函数fread从文件里读取数据到缓冲区Buff里。
#060      len = fread(Buff, 1, BUFFSIZE, stdin);
 
读取文件出错就退出。
#061      if (ferror(stdin)) {
#062        fprintf(stderr, "Read error\n");
#063        exit(-1);
#064      }
 
判断是否读取文件到结束。
#065      done = feof(stdin);
#066 
 
调用库函数XML_Parse来分析缓冲区Buff里的XML数据。
#067      if (! XML_Parse(p, Buff, len, done)) {
#068        fprintf(stderr, "Parse error at line %d:\n%s\n",
#069          XML_GetCurrentLineNumber(p),
#070          XML_ErrorString(XML_GetErrorCode(p)));
#071        exit(-1);
#072      }
#073 
 
如果分析文件到结尾位置,或者出错,就可以退出循环处理。
#074      if (done)
#075        break;
#076    }
#077 } /* End of main */
#078 
#079 
#080 
 
通过上面调用库函数XML_ParserCreate、XML_SetElementHandler、XML_Parse等三个函数就完成了XML的分析过程,这样使用起来真是太简单了,看到expat库的威力无穷

你可能感兴趣的:(使用Expat XML解析器的例子)