TinyXml 使用举例

    技术2022-05-19  34

    /*********************************************************************************  * author: hjjdebug  * date: 2011  * tinyxml, 有2个.h 文件, 4个cpp 文件。  * 对TinyXml 的理解,最好的办法是阅读代码  * xml 是一种层次状结构。xml可以用树内存结构描述。  * TinyXml 有几个重要概念。TinyXmlNode, TinyXmlElement, TinyXmlText, TinyXmlDocmnet  * 简述如下:  * TinyXmlDocmnet 是由TinyXmlNode 构成的树  * TinyXmlElement 继承自TinyXmlNode 并有私有变量TinyXmlAttribute ,  *                   属性可以例如pElement->SetAttribute("ID", "1") 设置ID=1 属性  * TinyXmlText 继承自TinyXmlNode 有自己的新接口函数  * 封装TinyXmlText TinyXmlElement 是为了更好的访问和控制TinyXmlNode 类。  * 整个TinyXml 构造核心是一个树。 更深的理解请阅读源码。  * 对xml 的使用就是建造一个个 元素, 或者阅读一个个元素, 元素是按层次构造的。  * 时刻注意元素(或者结点)的值, 注意元素和元素的关系  * 后面给出几个实例供参考,帮助使用和理解概念。  *********************************************************************************/ #include <windows.h> #include <vector> #include "tinyxml.h" #include "tinystr.h" #include <iostream> using namespace std; #pragma warning(disable:4996) typedef struct tagClipInfo {     int taskID;     vector<int> vecClipNo;     string targetName; }ClipInfo; char *xmlstr="/ <root>/     <taskid>15</taskid>/     <clips>/         <clip>1</clip>/         <clip>2</clip>/         <clip>3</clip>/         <clip>4</clip>/         <clip>5</clip>/     </clips>/     <output>/cbs/taskid/product/test.pcm</output>/ </root>/ "; /* 什么叫一个clip? 一个clip 就是包含了一个开始pts(可表示为videopts) 和一个结束pts(可表示为videopts )的结构 但这里的clip 仅仅是一个clip 的ID. ---------------------------------- input: xmlstr output: clipInfo ---------------------------------- */ bool ReadClipInfo(ClipInfo &clipInfo,char *xmlstr ) {     TiXmlDocument *pDoc = new TiXmlDocument();     pDoc->Parse(xmlstr);     //    pDoc->LoadFile(xmlFileName);     TiXmlElement *root = pDoc->RootElement();     if(!root)         return false;     TiXmlElement *taskidEle = root->FirstChildElement();     clipInfo.taskID = strtoul(taskidEle->GetText(),NULL,10);     TiXmlElement *clips = taskidEle->NextSiblingElement();     TiXmlElement *clip = clips->FirstChildElement();     while(clip)     {         int id=strtoul(clip->GetText(),NULL,10);         clipInfo.vecClipNo.push_back(id);         clip = clip->NextSiblingElement();     }     TiXmlElement *output = clips->NextSiblingElement();     clipInfo.targetName = output->GetText();     return true; } /* <root>     <taskid>200</taskid>//录制任务ID     <channel>15</channel> //频道ID     <stoptime>2011-1-13 09:00:00</stoptime> //录制任务结束时间 </root> */ BOOL  ReadXMLInfo(char *filename, int *taskID, int *channel, SYSTEMTIME &stopTime) {     char szStopTime[256];     TiXmlDocument* pDoc = new TiXmlDocument();     // pDoc->Parse(xmlString);     pDoc->LoadFile(filename);     TiXmlElement* root = pDoc->RootElement();      if(root==NULL)         return false;     TiXmlElement* pElement = root->FirstChildElement();     while(pElement)     {         if(strcmp(pElement->Value(),"taskid")==0)             *taskID=strtoul(pElement->GetText(),NULL,10);         else if(strcmp(pElement->Value(),"channel")==0)             *channel=strtoul(pElement->GetText(),NULL,10);         else if(strcmp(pElement->Value(),"stoptime")==0)         {             strcpy(szStopTime,pElement->GetText());             sscanf(szStopTime,"%d-%d-%d %d:%d:%d",&stopTime.wYear,&stopTime.wMonth,&stopTime.wDay,                 &stopTime.wHour,&stopTime.wMinute,&stopTime.wSecond);         }         pElement = pElement->NextSiblingElement();     }     delete(pDoc);     if(stopTime.wYear == 0)     {         cout << "error xml format, time is 0" <<endl;         system("pause");         return FALSE;     }     return TRUE; } /* 将该信息存储到filename <root>     <taskid>200</taskid>//录制任务ID     <channel>15</channel> //频道ID     <stoptime>2011-1-13 09:00:00</stoptime> //录制任务结束时间 </root> */ void WriteXmlFile(char *filename) {     TiXmlDocument * pDoc = new TiXmlDocument();     TiXmlElement *root = new TiXmlElement("root");     TiXmlElement *taskid =  new TiXmlElement("taskid");     TiXmlElement *channel =  new TiXmlElement("channel");     TiXmlElement *stoptime =  new TiXmlElement("stoptime");     TiXmlText *t_taskid = new TiXmlText("200");     taskid->LinkEndChild(t_taskid);     TiXmlText *t_channel = new TiXmlText("15");     channel->LinkEndChild(t_channel);     TiXmlText *t_stoptime = new TiXmlText("2011-1-13 09:00:00");     stoptime->LinkEndChild(t_stoptime);     pDoc->LinkEndChild(root);     root->LinkEndChild(taskid);     root->LinkEndChild(channel);     root->LinkEndChild(stoptime);     pDoc->SaveFile(filename);     delete(pDoc); } int main(int argc, char *argv[]) {     //测试读clipinfo     ClipInfo cInfo;     ReadClipInfo(cInfo, xmlstr);     cout << cInfo.targetName.c_str() << endl;     cout << cInfo.taskID << endl;     for(int i=0; i<sizeof(cInfo.vecClipNo.size()); i++)     {         cout << cInfo.vecClipNo[i]<<endl;     }     //测试生成一个xml字符串并写文件。     WriteXmlFile("test.xml");     //测试读录制任务     int taskID, channel;     SYSTEMTIME time;     ReadXMLInfo("test.xml", &taskID, &channel, time);     cout << "taskID = " << taskID <<endl;     cout << "channel = " << channel << endl;     cout << "time stucture:" << endl;     cout <<    "Year: "<<time.wYear << endl;     cout <<    "Month: " << time.wMonth << endl;     system("pause");     return 0; }


    最新回复(0)