Expat+SCEW-操弄XML的瑞士刀

前端之家收集整理的这篇文章主要介绍了Expat+SCEW-操弄XML的瑞士刀前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
09
04

Expat+SCEW-操弄XML的瑞士刀

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

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

Makefile的范例如下

  1. ALL@H_403_147@:example@H_403_147@
  2. @H_403_147@
  3. example@H_403_147@:example@H_403_147@.c@H_403_147@
  4. @H_403_147@$(CC)@H_403_147@-I@H_403_147@./scew@H_403_147@-0.4.0@H_403_147@/scew@H_403_147@/ -o@H_403_147@example@H_403_147@example@H_403_147@.c@H_403_147@libscew@H_403_147@.a@H_403_147@libexpat@H_403_147@.a@H_403_147@
  5. clean@H_403_147@:
  6. rm@H_403_147@example

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

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

  1. @H_403_147@<?xml@H_403_147@version@H_403_147@="1.0"@H_403_147@encoding@H_403_147@="UTF-8"@H_403_147@standalone@H_403_147@="no"@H_403_147@?>
  2. @H_403_147@
  3. @H_403_147@<scew_test@H_403_147@>
  4. @H_403_147@<element1@H_403_147@>element@H_403_147@contents@H_403_147@.</element1@H_403_147@>
  5. @H_403_147@<element2@H_403_147@attribute@H_403_147@="@H_403_147@/>
  6. @H_403_147@<element3@H_403_147@attribute1@H_403_147@="@H_403_147@attribute2@H_403_147@="@H_403_147@/>
  7. @H_403_147@<element4@H_403_147@>
  8. @H_403_147@<sub_element1@H_403_147@attribute@H_403_147@="@H_403_147@/>
  9. @H_403_147@<sub_element2@H_403_147@attribute1@H_403_147@="@H_403_147@>
  10. @H_403_147@<sub_sub_element1@H_403_147@attribute1@H_403_147@="@H_403_147@attribute3@H_403_147@="@H_403_147@>element@H_403_147@contents@H_403_147@.</sub_sub_element1@H_403_147@>
  11. @H_403_147@</sub_element2@H_403_147@>
  12. @H_403_147@</element4@H_403_147@>
  13. @H_403_147@</scew_test@H_403_147@>

而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. void@H_403_147@print_attributes(scew_element@H_403_147@*element)@H_403_147@
  2. {@H_403_147@
  3. scew_attribute@H_403_147@*attribute@H_403_147@=NULL@H_403_147@;
  4. @H_403_147@
  5. if@H_403_147@(element@H_403_147@!=NULL)@H_403_147@
  6. {@H_403_147@
  7. /**
  8. * Iterates through the element's attribute list,printing the
  9. * pair name-value.
  10. */@H_403_147@
  11. attribute@H_403_147@=NULL@H_403_147@;
  12. while@H_403_147@((attribute@H_403_147@=scew_attribute_next(element@H_403_147@,attribute))@H_403_147@!=NULL)@H_403_147@
  13. printf("%s=\"@H_403_147@%s@H_403_147@\""@H_403_147@,scew_attribute_name(attribute)@H_403_147@,scew_attribute_value(attribute))@H_403_147@;
  14. }@H_403_147@
  15. }@H_403_147@
  16. @H_403_147@
  17. @H_403_147@
  18. @H_403_147@
  19. int@H_403_147@PrintElement(scew_element@H_403_147@*element)@H_403_147@
  20. {@H_403_147@
  21. scew_element@H_403_147@*child@H_403_147@=NULL@H_403_147@;
  22. XML_Char@H_403_147@const@H_403_147@*contents@H_403_147@=NULL@H_403_147@;
  23. @H_403_147@
  24. printf("element name: %sscew_element_name(element))@H_403_147@;
  25. print_attributes(element)@H_403_147@;
  26. contents@H_403_147@=scew_element_contents(element)@H_403_147@;
  27. if@H_403_147@(contents@H_403_147@==NULL)@H_403_147@
  28. printf("\n\n")@H_403_147@;
  29. else@H_403_147@printf("\n%s content:%s\n\nscew_element_name(element)@H_403_147@,contents)@H_403_147@;
  30. @H_403_147@
  31. /**
  32. * Call print_element function again for each child of the
  33. * current element.
  34. */@H_403_147@
  35. while@H_403_147@((child@H_403_147@=scew_element_next(element@H_403_147@,child))@H_403_147@!=NULL)@H_403_147@
  36. PrintElement(child)@H_403_147@;
  37. @H_403_147@
  38. return@H_403_147@0@H_403_147@;
  39. @H_403_147@
  40. }@H_403_147@
  41. int@H_403_147@ReadXML()@H_403_147@
  42. {@H_403_147@
  43. scew_tree@H_403_147@*tree@H_403_147@=NULL@H_403_147@;
  44. scew_parser@H_403_147@*parser@H_403_147@=NULL@H_403_147@;
  45. scew_element@H_403_147@*element@H_403_147@=NULL@H_403_147@,*parent@H_403_147@=NULL@H_403_147@;
  46. /**
  47. * Creates an SCEW parser. This is the first function to call.
  48. */@H_403_147@
  49. parser@H_403_147@=scew_parser_create()@H_403_147@;
  50. scew_parser_ignore_whitespaces(parser@H_403_147@,1)@H_403_147@;
  51. if@H_403_147@(@H_403_147@!scew_parser_load_file(parser@H_403_147@,0)">"))@H_403_147@return@H_403_147@0@H_403_147@;
  52. tree@H_403_147@=scew_parser_tree(parser)@H_403_147@;
  53. PrintElement(scew_tree_root(tree))@H_403_147@;//traverse all child and siblings of tree
  54. /* Remember to free tree (scew_parser_free does not free it) */@H_403_147@
  55. scew_tree_free(tree)@H_403_147@;
  56. @H_403_147@
  57. /* Frees the SCEW parser */@H_403_147@
  58. scew_parser_free(parser)@H_403_147@;
  59. return@H_403_147@0@H_403_147@;

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

评论:0|引用: 0 |阅读: 5740

猜你在找的XML相关文章