c++ 解析xml文件
生活随笔
收集整理的這篇文章主要介紹了
c++ 解析xml文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解析的xml文件
<?xml version="1.0" encoding="utf-8"?><Objects><person><label>15</label><xmin>207</xmin><ymin>106</ymin><xmax>489</xmax><ymax>381</ymax><score>0.99566</score></person><person><label>15</label><xmin>546</xmin><ymin>57</ymin><xmax>912</xmax><ymax>395</ymax><score>0.984854</score></person><person><label>15</label><xmin>898</xmin><ymin>103</ymin><xmax>1223</xmax><ymax>382</ymax><score>0.958104</score></person><person><label>15</label><xmin>2</xmin><ymin>61</ymin><xmax>381</xmax><ymax>389</ymax><score>0.906186</score></person></Objects>c++文件 #include <string> #include <string.h> #include <stdio.h> #include "stdlib.h" #include <libxml/xpath.h> #include <libxml/parser.h> #include <libxml/xmlmemory.h> #include <libxml/xmlstring.h> #include <jqw.h>int main() {objects obj; double fps;xmlDocPtr pDoc = xmlReadFile("parameter.xml", "UTF-8", XML_PARSE_RECOVER); //獲取XML文檔的指針if(NULL == pDoc){fprintf(stderr, "xmlParseFile Error in %s %d\n",__FUNCTION__, __LINE__);}xmlNodePtr pRoot = xmlDocGetRootElement(pDoc);//獲取根節點if(NULL == pRoot){fprintf(stderr, "xmlDocGetRootElement Error in %s %d\n", __FUNCTION__, __LINE__);xmlFreeDoc(pDoc);}printf("Node name is %s!\n", pRoot->name);int i=0;xmlNodePtr pFirst = pRoot->xmlChildrenNode;//獲取子節點while(NULL != pFirst){//printf("---------------i= %d\n", i);if(!xmlStrcmp(pFirst->name, (const xmlChar *)("person"))){xmlNodePtr pSecond = pFirst->xmlChildrenNode;while(NULL != pSecond){xmlChar* value= NULL;if(!xmlStrcmp(pSecond->name, (const xmlChar *)("label"))){i=i+1;value = xmlNodeGetContent(pSecond);//printf("\n%s-->%s\n", pSecond->name, value);char * stream = (char *) value;obj.p[i].lable=atoi(stream);printf("obj.person[%d].lable=%d\n",i,obj.p[i].lable);stream=NULL;xmlFree(value);value = NULL;}if(!xmlStrcmp(pSecond->name, (const xmlChar *)("xmin"))){value = xmlNodeGetContent(pSecond);//printf("\n%s-->%s\n", pSecond->name, value);char * stream = (char *) value;obj.p[i].xmin=atoi(stream);printf("obj.person[%d].xmin=%d\n",i,obj.p[i].xmin);stream=NULL;xmlFree(value);value = NULL;}if(!xmlStrcmp(pSecond->name, (const xmlChar *)("ymin"))){value = xmlNodeGetContent(pSecond);//printf("\n%s-->%s\n", pSecond->name, value);char * stream = (char *) value;obj.p[i].ymin=atoi(stream);printf("obj.person[%d].ymin=%d\n",i,obj.p[i].ymin);stream=NULL;xmlFree(value);value = NULL;}if(!xmlStrcmp(pSecond->name, (const xmlChar *)("xmax"))){value = xmlNodeGetContent(pSecond);//printf("\n%s-->%s\n", pSecond->name, value);char * stream = (char *) value;obj.p[i].xmax=atoi(stream);printf("obj.person[%d].xmax=%d\n",i,obj.p[i].xmax);stream=NULL;xmlFree(value);value = NULL;}if(!xmlStrcmp(pSecond->name, (const xmlChar *)("ymax"))){value = xmlNodeGetContent(pSecond);//printf("\n%s-->%s\n", pSecond->name, value);char * stream = (char *) value;obj.p[i].ymax=atoi(stream);printf("obj.person[%d].ymax=%d\n",i,obj.p[i].ymax);stream=NULL;xmlFree(value);value = NULL;}if(!xmlStrcmp(pSecond->name, (const xmlChar *)("score"))){value = xmlNodeGetContent(pSecond);char * stream = (char *) value;//printf("\n%s-->%s\n", pSecond->name, value);obj.p[i].score=atof(stream);printf("obj.person[%d].score=%f\n",i,obj.p[i].score);stream=NULL;xmlFree(value);value = NULL;}pSecond = pSecond->next;}}pFirst = pFirst->next;}obj.personnum=i;printf("obj.personnum=%d\n",obj.personnum);xmlFreeDoc(pDoc);return 0; } jqw.h #ifndef __JQW_H__ #define __JQW_H__struct person{int lable;int xmin;int ymin;int xmax;int ymax;float score; };struct objects {person p[3];int personnum; }; #endif編譯腳本 #!/bin/bash # #gcc -I/usr/include/libxml2 parse_xml.cpp -o CreateXmlFile -L /usr/lib/i386-linux-gnu -lxml2 echo "finish compile"echo "start run pragram!!!"exit 0
總結
以上是生活随笔為你收集整理的c++ 解析xml文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot Serverle
- 下一篇: 阿里 蚂蚁自研 IDE 研发框架 Op