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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值