xmlDocPtr parseDoc(char *docname, char *keyword) { xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseFile(docname); if (doc == NULL) { fprintf(stderr, "Documents not parsed successfully.\n"); return NULL; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr, "empty documents\n"); xmlFreeDoc(doc); return NULL; } if (xmlStrcmp(cur->name, (const xmlChar *)"story")) { fprintf(stderr, "document os the wrong type, root node != story"); xmlFreeDoc(doc); return NULL; } cur = cur->xmlChildrenNode; while (cur) { if ( (!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))) { parseStory(doc, cur, keyword); } cur = cur->next; } return doc; }
int main(void) { xmlDocPtr doc; xmlNodePtr cur; char *docmem; docmem = createdoc(); doc = xmlParseMemory(docmem, strlen(docmem)); if (doc == NULL) { return; } cur = xmlDocGetRootElement(doc); cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *) "entry"))) { parseStory(doc, cur); } cur = cur->next; } xmlSaveFormatFile("spread.xml", doc, 1); xmlFreeDoc(doc); xmlCleanupParser(); }
static void parseDoc(char *docName) { xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseFile(docName); if(NULL == doc){ printf("input a NULL file name\n"); return ; } cur = xmlDocGetRootElement(doc); if(NULL == cur){ printf("empty document\n"); xmlFreeDoc(doc); return ; } cur = cur->xmlChildrenNode; while(cur != NULL){ if(!xmlStrcmp(cur->name, (const xmlChar *)"user")){ parseStory(doc, cur); } cur = cur->next; } xmlFreeDoc(doc); return; }
int main(int argc, char **argv) { xmlDocPtr doc = NULL; //定义解析文档指针 xmlNodePtr cur = NULL; //定义结点指针(你需要它为了在各个结点间移动) xmlNodePtr child = NULL; xmlChar *key = NULL; char *psfilename = NULL; xmlChar *value = NULL; #if 0 if (argc < 1) { printf("error: Usage: %s filename/n" , argv[0]); return; } psfilename = argv[1]; #endif doc = xmlParseFile("w.xml"); //doc = xmlReadFile(psfilename, MY_ENCODING, 256); //解析文件 if (doc == NULL ) { fprintf(stderr,"Document not parsed successfully. \n"); return; } cur = xmlDocGetRootElement(doc); //确定文档根元素 if (cur == NULL)/*检查确认当前文档中包含内容*/ { fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return; } if (xmlStrcmp(cur->name, (const xmlChar *)"socket"))//roor node--socket { fprintf(stderr,"document of the wrong type, socket node != socket"); xmlFreeDoc(doc); return; } printf("root node: %s\n", cur->name); cur = cur->xmlChildrenNode;//socket_type while(cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"socket_type"))) { value = xmlGetProp(cur, (const xmlChar *)"type");//attributeaa printf("socket_type: %s\n", value); parseStory(doc, cur); xmlFree(value); } cur = cur->next; } xmlSaveFormatFileEnc("w.xml", doc, "UTF-8", 1); xmlFreeDoc(doc); return(0); }
void parseStory (xmlDocPtr doc, xmlNodePtr cur, xmlChar **path) { /* first output should be /storyinfo/author/John */ xmlNodePtr level; xmlChar *path2[1000]; xmlChar *myfile; xmlChar *contents; FILE *file = NULL; file = fopen("/root/Desktop/Project/fuse-2.6.3/example/path.txt", "a"); //if it is a file or an empty directory handle ending differently if(((cur->xmlChildrenNode->next)==NULL)){ strcat(path, cur->name); contents = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); if((xmlStrcmp(contents, " "))){ strcat(path, ":"); } else{ strcat(path, "/"); } } else{ strcat(path, cur->name); strcat(path, "/"); } fprintf(file, "%s\n", path); fclose(file); //printf("%s\n", path); /* check and see if there is a child. if not get file, if so continue normally*/ if(((cur->xmlChildrenNode->next)==NULL)){ fopen("/root/Desktop/Project/fuse-2.6.3/example/path.txt", "a"); contents = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); if((xmlStrcmp(contents, " "))){ fprintf(file, "%s\n<<>>\n", contents); } fclose(file); //printf("%s\n", path); } else{ level = cur->xmlChildrenNode->next; while(level!=NULL){ strcpy(path2, path); parseStory(doc, level, path2); level = level->next->next; } } //printf("cur: %s\n", cur->name); //printf("next cur: %s\n", cur->next->name); }
int parseDoc() { xmlDocPtr doc; xmlNodePtr cur; // point to the saved xml file doc = xmlParseFile(savefile); if (doc == NULL) { printf("xml parse file error\n"); return(1); } cur = xmlDocGetRootElement(doc); if (cur == NULL) { printf("xml empty document error\n"); return(1); } // first check if root element of doc is correct // "feed" is docroot of weather xml file if (xmlStrcmp(cur->name, (const xmlChar *) "feed")) { printf("xml doc of wrong type\n"); xmlFreeDoc(doc); return(1); } // then pick out each sub-element you want, and zero in on each inner element using parseStory (above) cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *) "entry"))) { parseStory(doc, cur); } cur = cur->next; } xmlFreeDoc(doc); // now print the weather items int a; for (a = 0; a < x; a++) { printf("%d: %s\n", a, theitems[a]); } // delete xml file after use and end program remove(savefile); return(0); }
static void parseDoc(char *docname) { xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseFile(docname); if (doc == NULL ) { fprintf(stderr,"Document not parsed successfully. \n"); return; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return; } /* printf("%s\n", (char*) cur->name); */ if (xmlStrcmp(cur->name, (const xmlChar *) "document-content")) { /* ROOT NODE (story) */ fprintf(stderr,"document of the wrong type, root node != story\n"); xmlFreeDoc(doc); return; } cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"body"))){ /* CHILD NODE (storyinfo) */ cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"text"))) parseStory (doc, cur); cur = cur->next; } //parseStory (doc, cur); } if (cur != NULL) cur = cur->next; } xmlFreeDoc(doc); return; }
static void parseDoc(char *docname) { xmlDocPtr doc; xmlNodePtr cur; xmlChar *path[1000]; xmlChar* mylevel; doc = xmlParseFile(docname); if (doc == NULL ) { fprintf(stderr,"Document not parsed successfully. \n"); return; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return; } if (xmlStrcmp(cur->name, (const xmlChar *) "root")) { fprintf(stderr,"document of the wrong type, root node != root"); xmlFreeDoc(doc); return; } cur = cur->xmlChildrenNode; mylevel = cur->next->name; //printf("mylevel: %s\n", mylevel); while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)mylevel))){ //printf("mycurname: %s\n", cur->name); strcpy(path, "/"); parseStory (doc, cur, path); if((cur->next->next)!=NULL){ strcpy(path, "/"); mylevel=cur->next->next->name; } } cur = cur->next; } xmlFreeDoc(doc); return; }