2015年10月14日 星期三 18:09
比如这个
<configure>
<value>hello</value>
<name>this is hello's name</name>
<value>hi</value>
<name>this is hi's name</name>
<value>haha</value>
<name>this is haha's name</name>
</configure>
2015年10月14日 星期三 18:43
这是我的想法:
#include<stdio.h>
#include<stdlib.h>
#include<libxml/xmlmemory.h>
#include<libxml/parser.h>
/* <!-- gcc $1 -o DisplayXml -lxml2 -->
* <configure>
* <value>first</value>
* <name>first's name</name>
* <value>second</value>
* <name>second's name</name>
* </configure>
*/
int main(int argc,char * argv[]){
xmlDocPtr doc; //the document statement
xmlNodePtr cur; //the xmlNode statement
doc = xmlParseFile(argv[1]); //push the xml File into doc
cur = xmlDocGetRootElement(doc); //get the xml ROOT value by cur
if(doc == NULL){ //Is doc (xml design) right?TRUE->next:FALSE->kill;
fprintf(stderr,"Document not parsed successfully\n");
xmlFreeDoc(doc);
return -1;
}
if(cur == NULL){ //Is xml value full?TRUE->next:FALSE->kill;
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
}
if(xmlStrEqual(cur->name,(const xmlChar *)"configure")){
printf("document of the true type, root node is configure\n");
xmlChar *value; //The value statement
xmlChar *name; //The name statement
cur = cur->xmlChildrenNode; //The Root's children
while(cur != NULL){
if(!xmlStrcmp(cur->name,(const xmlChar*)"value")){
/*the API say's that xmlNStrEqual() is faster than xmlStrcmp()*/
value = xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
printf("value is: %s\n",value); //printf the value
xmlFree(value);
}
if(!xmlStrcmp(cur->name,(const xmlChar*)"name")){
name = xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
printf("name is: %s\n",name); //printf the name
xmlFree(name);
}
cur = cur->next;
}
}
return (0);
}
2018年05月21日 星期一 10:32
用libxml2能解决吧
Zeuux © 2024
京ICP备05028076号