解析的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