- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <libxml/xmlmemory.h>
- #include <libxml/parser.h>
- #include <libxml/tree.h>
- void
- get_connnectrequestUrl(char *docname) {
- xmlDocPtr doc;
- xmlNodePtr cur;
- doc = xmlParseFile(docname);
- if (doc == NULL ) {
- printf("Document not parsed successfully. \n");
- return;
- }
- cur = xmlDocGetRootElement(doc);
- if (cur == NULL) {
- printf("empty document\n");
- xmlFreeDoc(doc);
- return;
- }
- if (xmlStrcmp(cur->name, (const xmlChar *) "Context")) {
- printf("document of the wrong type, root node != Context");
- xmlFreeDoc(doc);
- return;
- }
- cur = cur->xmlChildrenNode;
- while (cur != NULL){
- xmlNodePtr cur2_tmp = cur;
- xmlChar *key;
- xmlChar *attr;
- char connectrequestUrl[32];
- if ((!xmlStrcmp(cur->name, (const xmlChar *)"list"))){
- attr = xmlNodeListGetString(doc, cur->properties->xmlChildrenNode, 1);
- if((!xmlStrcmp(attr, (const xmlChar *)"rrm_cell"))){
- xmlFree(attr);
- cur = cur->xmlChildrenNode;
- while(cur != NULL){
- xmlNodePtr cur2_tmp = cur;
- if ((!xmlStrcmp(cur->name, (const xmlChar *)"item"))){
- attr = xmlNodeListGetString(doc, cur->properties->xmlChildrenNode, 1);
- if((!xmlStrcmp(attr, (const xmlChar *)"Cell_Map_s"))){
- xmlFree(attr);
- cur = cur->xmlChildrenNode;
- while(cur != NULL){
- xmlNodePtr cur2_tmp = cur;
- if ((!xmlStrcmp(cur->name, (const xmlChar *)"p"))){
- attr = xmlNodeListGetString(doc, cur->properties->xmlChildrenNode, 1);
- if((!xmlStrcmp(attr, (const xmlChar *)"Hnb_IP"))){
- xmlFree(attr);
- printf("********************\n");
- key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
- memcpy(connectrequestUrl,(const char *)key,strlen((const char *)key));
- connectrequestUrl[strlen((const char *)key)]='\0';
- printf("the url is:%s\n",connectrequestUrl);
- xmlFree(key);
- }
- else{
- xmlFree(attr);
- }
- }
- /*reset cul*/
- cur = cur2_tmp;
- cur = cur -> next;
- }
- }
- else{
- xmlFree(attr);
- }
- }
- /*RESET cur*/
- cur = cur2_tmp;
- cur = cur -> next;
- }
- }
- else{
- xmlFree(attr);
- }
- }
- cur = cur2_tmp;
- cur = cur -> next;
- }
- xmlFreeDoc(doc);
- }
- int main(void){
- while(1){
- get_connnectrequestUrl("config.xml");
- sleep(5);
- //return 0;
- }
- }
编译:gcc -o testxml4 testxml4.c -I /usr/local/include/libxml2/ -L /usr/local/lib -lxml2
使用valgrind检测内存泄露的情况:valgrind --tool=memcheck --leak-check=full ./testxml4
交叉编译: arm-none-linux-gnueabi-gcc -o testxml4 testxml4.c -I /usr/local/include/libxml2/ -L /usr/local/lib -lxml2