|
楼主 |
发表于 2005-3-5 19:43:39
|
显示全部楼层
以下是测试用的example.c和example.xml, 在vc和gcc中编译通过,我把它也放在了上面的包里.
[code:1]
#include "Cxmlread.h"
void explore_tree(NODE *tree)
{
int count=0;
do {
printf("name:%s,needvalue:%d,attrnum:%d\n",\
tree->name,tree->needvalue,tree->attrnum);
if(tree->attrnum)
do
printf("attr:%d %s\n",count,tree->attr[count]->name);
while(++count<tree->attrnum);
printf("_______\n");
if(tree->child)
{printf("child:%s\n",tree->child->name);explore_tree(tree->child);}
}while(tree=tree->brother);
}
void printresult(struct result *temp)
{
int count=0;
while(NULL!=temp)
{ printf("result %d: %s\n",count++,temp->value);
temp=temp->next;
}
}
int main()
{
FILE *fp;
NODE *root,*project,*name,*tree,*path;
tree=xml_init();
fp=fopen("example.xml","r");
if(!fp){
printf("file not found!");
exit(0);
}
root=getnodeattr(1,"/root",tree,"type");
project=getnodeattr(2,"/root/project",tree,"manager","statues");
name=getnodevalue("/root/project/name",tree);
path=getnodevalue("/root/project/path",tree);
getnodeattr(1,"/root/project/path",tree,"host");
explore_tree(tree);
printf("==============\n\n");
xmlread(fp,tree);
fclose(fp);
printf("/root attr[0]: %s\n",root->attr[0]->name);
printresult(root->attr[0]->result);
printf("/root/project attr[0]: %s\n",project->attr[0]->name);
printresult(project->attr[0]->result);
printf("/root/project attr[1]: %s\n",project->attr[1]->name);
printresult(project->attr[1]->result);
printf("/root/project/name\n");
printresult(name->result);
printf("/root/project/path\n");
printresult(path->result);
printf("/root/project/path attr[0]: %s\n",path->attr[0]->name);
printresult(path->attr[0]->result);
cleanresult(tree,1);
//清除结果
}
[/code:1]
example.xml
[code:1]
<?xml version="1.0" encoding="GB2312"?>
<!-- 这是一个不规范、无意义的文档,测试程序的容错能力 -->
<root type="test" encording="zh_CN.GB2312">
<project statues="open" manager="M" startime="2004-1-1">
<name>1</name>
<type>C</type>
<path host="No.1">/var/d</path>
</project>
<project statues="close" manager="J" startime="2004-2-4">
<name >2</name>
<type>java</type>
<path >/var/java</path>
</project>
<project startime="2004-?-?" manager="k" >
<!--project的属性statues和元素name不存在,会自动跳过。-->
<type>java</type>
<path host="local">/var/java1</path>
</project>
<project >
<name/><!--name没有结束标签,不会读取它的值,其array_size不增加-->
<type></type>
<path></path><!--path没有值,返回结果会增加一个长度为0的value字符串-->
</project>
</root>
[/code:1]
vc的输出:
name:root,needvalue:0,attrnum:1
attr:0 type
_______
child:project
name:project,needvalue:0,attrnum:2
attr:0 manager
attr:1 statues
_______
child:name
name:name,needvalue:1,attrnum:0
_______
name:path,needvalue:1,attrnum:1
attr:0 host
_______
==============
/root attr[0]: type
result 0: test
/root/project attr[0]: manager
result 0: M
result 1: J
result 2: k
/root/project attr[1]: statues
result 0: open
result 1: close
/root/project/name
result 0: 1
result 1: 2
/root/project/path
result 0: /var/d
result 1: /var/java
result 2: /var/java1
result 3:
/root/project/path attr[0]: host
result 0: No.1
result 1: local
Press any key to continue |
|