09
04
|
Expat+SCEW-操弄XML的瑞士刀作者: Joey 日期: 2008-09-04 18:38 |
之前处理XML文件时,就是用这套工具横行江湖,Expat提供细致的函式读写xml文件,SCEW则是把Expat函式包装成亮丽的界面供使用者更方便的存取xml,个人觉的,这两套函式库实在不输给.net System.xml下的API
首先下载expat library和scew library,这两套软体的使用方式很简单,执行configure,make,make install后,就可以使用它们的library,而我这边的范例编译时用static link,所以我都直接连接它们的.a函式库档
Makefile的范例如下
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文件的范例如下
- intCreateXML@H_403_179@()
- @H_403_179@{
- scew_tree*tree=NULL;
- scew_element*root=NULL;
- scew_element*element=NULL;
- scew_element*sub_element=NULL;
- scew_element*sub_sub_element=NULL;
- scew_attribute*attribute=NULL;
- tree=scew_tree_create@H_403_179@();
- root=scew_tree_add_root@H_403_179@(tree,"scew_test"@H_403_179@);
- /* Add an element and set element contents. */
- element=scew_element_add@H_403_179@(root,0)">"element1"@H_403_179@);
- scew_element_set_contents@H_403_179@(element,0)">"element contents."@H_403_179@);
- /* Add an element with an attribute pair (name,value). */
- element=scew_element_add@H_403_179@(root,0)">"element2"@H_403_179@);
- scew_element_add_attr_pair@H_403_179@(element,0)">"attribute",0)">"value"@H_403_179@);
- element=scew_element_add@H_403_179@(root,0)">"element3"attribute1"value1"@H_403_179@);
- /**
- * Another way to add an attribute. You loose attribute ownership,
- * so there is no need to free it.
- */
- attribute=scew_attribute_create@H_403_179@("attribute2"value2"@H_403_179@);
- scew_element_add_attr@H_403_179@(element,attribute@H_403_179@);
- element=scew_element_add@H_403_179@(root,0)">"element4"@H_403_179@);
- sub_element=scew_element_add@H_403_179@(element,0)">"sub_element1"@H_403_179@);
- scew_element_add_attr_pair@H_403_179@(sub_element,0)">"@H_403_179@);
- sub_element=scew_element_add@H_403_179@(element,0)">"sub_element2"@H_403_179@);
- sub_sub_element=scew_element_add@H_403_179@(sub_element,0)">"sub_sub_element1"@H_403_179@);
- scew_element_add_attr_pair@H_403_179@(sub_sub_element,0)">"attribute3"value3"@H_403_179@);
- /* Check attribute2 replacement. */
- scew_element_add_attr_pair@H_403_179@(sub_sub_element,0)">"new_value2"@H_403_179@);
- scew_element_set_contents@H_403_179@(sub_sub_element,0)">"@H_403_179@);
- scew_writer_tree_file@H_403_179@(tree,0)">"example.xml"@H_403_179@);
- scew_tree_free@H_403_179@(tree@H_403_179@);
- return0;
- @H_403_179@}
执行范例会制造出如下内容的xml
- <?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
- <scew_test>
- <element1>elementcontents.</element1>
- <element2attribute="/>
- <element3attribute1="attribute2="/>
- <element4>
- <sub_element1attribute="/>
- <sub_element2attribute1=">
- <sub_sub_element1attribute1="attribute3=">elementcontents.</sub_sub_element1>
- </sub_element2>
- </element4>
- </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文件的范例如下
- voidprint_attributes@H_403_179@(scew_element*element@H_403_179@)
- @H_403_179@{
- scew_attribute*attribute=NULL;
- if@H_403_179@(element!=NULL@H_403_179@)
- @H_403_179@{
- /**
- * Iterates through the element's attribute list,printing the
- * pair name-value.
- */
- attribute=NULL;
- while@H_403_179@((attribute=scew_attribute_next@H_403_179@(element,attribute@H_403_179@))!=NULL@H_403_179@)
- printf@H_403_179@("%s=\"%s\"",scew_attribute_name@H_403_179@(attribute@H_403_179@),scew_attribute_value@H_403_179@(attribute@H_403_179@));
- @H_403_179@}
- @H_403_179@}
- intPrintElement@H_403_179@(scew_element*element@H_403_179@)
- @H_403_179@{
- scew_element*child=NULL;
- XML_Charconst*contents=NULL;
- printf@H_403_179@("element name: %sscew_element_name@H_403_179@(element@H_403_179@));
- print_attributes@H_403_179@(element@H_403_179@);
- contents=scew_element_contents@H_403_179@(element@H_403_179@);
- if@H_403_179@(contents==NULL@H_403_179@)
- printf@H_403_179@("\n\n"@H_403_179@);
- elseprintf@H_403_179@("\n%s content:%s\n\nscew_element_name@H_403_179@(element@H_403_179@),contents@H_403_179@);
- /**
- * Call print_element function again for each child of the
- * current element.
- */
- while@H_403_179@((child=scew_element_next@H_403_179@(element,child@H_403_179@))!=NULL@H_403_179@)
- PrintElement@H_403_179@(child@H_403_179@);
- return0;
- @H_403_179@}
- intReadXML@H_403_179@()
- @H_403_179@{
- scew_tree*tree=NULL;
- scew_parser*parser=NULL;
- scew_element*element=NULL,*parent=NULL;
- /**
- * Creates an SCEW parser. This is the first function to call.
- */
- parser=scew_parser_create@H_403_179@();
- scew_parser_ignore_whitespaces@H_403_179@(parser,1@H_403_179@);
- if@H_403_179@(!scew_parser_load_file@H_403_179@(parser,0)">"@H_403_179@))return0;
- tree=scew_parser_tree@H_403_179@(parser@H_403_179@);
- PrintElement@H_403_179@(scew_tree_root@H_403_179@(tree@H_403_179@));//traverse all child and siblings of tree
- /* Remember to free tree (scew_parser_free does not free it) */
- scew_tree_free@H_403_179@(tree@H_403_179@);
- /* Frees the SCEW parser */
- scew_parser_free@H_403_179@(parser@H_403_179@);
- return0;
完整的范例程式请点此下载
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)