加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Expat+SCEW-操弄XML的瑞士刀

发布时间:2020-12-15 23:03:17 所属栏目:百科 来源:网络整理
导读:爱做梦的芦荟 Day Dreamer's Blog http://download.savannah.gnu.org/releases/scew/ 首页 标签 留言板 连结 归档 管理 发表 RSS 订阅| 上一篇| 返回| 下一篇 程式设计 09 04 Expat+SCEW-操弄XML的瑞士刀 作者: Joey 日期: 2008-09-04 18:38 字体大小:小中大
订阅| 上一篇| 返回| 下一篇
程式设计
09
04

Expat+SCEW-操弄XML的瑞士刀

之前处理XML文件时,就是用这套工具横行江湖,Expat提供细致的函式读写xml文件,SCEW则是把Expat函式包装成亮丽的界面供使用者更方便的存取xml,个人觉的,这两套函式库实在不输给.net System.xml下的API

首先下载expat library和scew library,这两套软体的使用方式很简单,执行configure,make,make install后,就可以使用它们的library,而我这边的范例编译时用static link,所以我都直接连接它们的.a函式库档

Makefile的范例如下

  1. ALL:example
  2. example:example.c
  3. $(CC)-I./scew-0.4.0/scew/ -oexampleexample.clibscew.alibexpat.a
  4. clean:
  5. rmexample

SCEW write xml的方法大概就五组API如下
(1)创造xml tree: tree = scew_tree_create();
(2)加入root element: scew_tree_add_root(scew_tree*,char*);
(3)加入root element的子element: scew_element_add(scew_element*,char*);
(4)加入element attribute: scew_element_add_attr_pair(scew_element*,char*,char*);;
(5)加入element content: scew_element_set_contents(scew_element*,char *);

写入xml文件的范例如下

  1. intCreateXML()
  2. {
  3. scew_tree*tree=NULL;
  4. scew_element*root=NULL;
  5. scew_element*element=NULL;
  6. scew_element*sub_element=NULL;
  7. scew_element*sub_sub_element=NULL;
  8. scew_attribute*attribute=NULL;
  9. tree=scew_tree_create();
  10. root=scew_tree_add_root(tree,"scew_test");
  11. /* Add an element and set element contents. */
  12. element=scew_element_add(root,0)">"element1");
  13. scew_element_set_contents(element,0)">"element contents.");
  14. /* Add an element with an attribute pair (name,value). */
  15. element=scew_element_add(root,0)">"element2");
  16. scew_element_add_attr_pair(element,0)">"attribute",0)">"value");
  17. element=scew_element_add(root,0)">"element3"attribute1"value1");
  18. /**
  19. * Another way to add an attribute. You loose attribute ownership,
  20. * so there is no need to free it.
  21. */
  22. attribute=scew_attribute_create("attribute2"value2");
  23. scew_element_add_attr(element,attribute);
  24. element=scew_element_add(root,0)">"element4");
  25. sub_element=scew_element_add(element,0)">"sub_element1");
  26. scew_element_add_attr_pair(sub_element,0)">");
  27. sub_element=scew_element_add(element,0)">"sub_element2");
  28. sub_sub_element=scew_element_add(sub_element,0)">"sub_sub_element1");
  29. scew_element_add_attr_pair(sub_sub_element,0)">"attribute3"value3");
  30. /* Check attribute2 replacement. */
  31. scew_element_add_attr_pair(sub_sub_element,0)">"new_value2");
  32. scew_element_set_contents(sub_sub_element,0)">");
  33. scew_writer_tree_file(tree,0)">"example.xml");
  34. scew_tree_free(tree);
  35. return0;
  36. }

执行范例会制造出如下内容的xml

  1. <?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
  2. <scew_test>
  3. <element1>elementcontents.</element1>
  4. <element2attribute="/>
  5. <element3attribute1="attribute2="/>
  6. <element4>
  7. <sub_element1attribute="/>
  8. <sub_element2attribute1=">
  9. <sub_sub_element1attribute1="attribute3=">elementcontents.</sub_sub_element1>
  10. </sub_element2>
  11. </element4>
  12. </scew_test>

而SCEW读取xml的方法大约有6组API
(1)创造xml parser: parser = scew_parser_create;
(2)读入xml档案: scew_parser_load_file(scew_parser*,char*)
(3)读出element name: scew_element_name(scew_element*)
(4)读出element attribute: scew_attribute_next(scew_element*,scew_attribute*)
(5)读出element content: scew_element_contents(scew_element*)
(6)寻找子element: scew_element_next(scew_element*,scew_element*)

读取xml文件的范例如下

  1. voidprint_attributes(scew_element*element)
  2. {
  3. scew_attribute*attribute=NULL;
  4. if(element!=NULL)
  5. {
  6. /**
  7. * Iterates through the element's attribute list,printing the
  8. * pair name-value.
  9. */
  10. attribute=NULL;
  11. while((attribute=scew_attribute_next(element,attribute))!=NULL)
  12. printf("%s="%s"",scew_attribute_name(attribute),scew_attribute_value(attribute));
  13. }
  14. }
  15. intPrintElement(scew_element*element)
  16. {
  17. scew_element*child=NULL;
  18. XML_Charconst*contents=NULL;
  19. printf("element name: %sscew_element_name(element));
  20. print_attributes(element);
  21. contents=scew_element_contents(element);
  22. if(contents==NULL)
  23. printf("nn");
  24. elseprintf("n%s content:%snnscew_element_name(element),contents);
  25. /**
  26. * Call print_element function again for each child of the
  27. * current element.
  28. */
  29. while((child=scew_element_next(element,child))!=NULL)
  30. PrintElement(child);
  31. return0;
  32. }
  33. intReadXML()
  34. {
  35. scew_tree*tree=NULL;
  36. scew_parser*parser=NULL;
  37. scew_element*element=NULL,*parent=NULL;
  38. /**
  39. * Creates an SCEW parser. This is the first function to call.
  40. */
  41. parser=scew_parser_create();
  42. scew_parser_ignore_whitespaces(parser,1);
  43. if(!scew_parser_load_file(parser,0)">"))return0;
  44. tree=scew_parser_tree(parser);
  45. PrintElement(scew_tree_root(tree));//traverse all child and siblings of tree
  46. /* Remember to free tree (scew_parser_free does not free it) */
  47. scew_tree_free(tree);
  48. /* Frees the SCEW parser */
  49. scew_parser_free(parser);
  50. return0;

完整的范例程式请点此下载

标签:linux
评论:0|引用: 0 |阅读: 5740
显示Tag关联文章
Android on QT2410-自己搞自己(2010-02-04 17:41)
Regular expression-跟brainfuck差不多的东西(2009-11-13 15:37)
Reading file in kernel-简单但实用(2009-10-13 15:18)
Linux file system for dummies-只花你45分钟(2009-08-19 15:40)
OPENSSL-TCP SSL初心者之路(2009-07-16 15:16)
NAPI与pure interrupt driver的效能比较(2009-04-29 19:06)
usermode helper-来自kernel的呼唤(2009-04-21 16:19)
kernel module memory detector-抓出有害的kernel module (2009-03-31 13:50)
kernel space coding-如履薄冰(2009-03-26 09:52)
readahead与posix_advise-预读取是万能灵丹? (2009-03-06 15:54)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读