TinyXml使用实例

来源:互联网 发布:长短经哪个版本好 知乎 编辑:程序博客网 时间:2024/06/14 02:59
[cpp] view plaincopy
  1. #include<iostream>  
  2. #include <string.h>  
  3. #include <cstdlib>  
  4.   
  5. #include "tinystr.h"  
  6. #include "tinyxml.h"  
  7.   
  8. using namespace std;  
  9.   
  10. bool GetNodePointerByName(TiXmlElement* pRootEle, const char* strNodeName,TiXmlElement* &destNode)    
  11. {  //根据结点名获取结点指针  
  12.     // 假如等于根节点名,就退出   
  13.     if (0 == strcmp(strNodeName, pRootEle->Value()))    
  14.     {    
  15.         destNode = pRootEle;    
  16.         return true;    
  17.     }    
  18.   
  19.     TiXmlElement* pEle = pRootEle;      
  20.     for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())      
  21.     {      
  22.         // 递归处理子节点,获取节点指针      
  23.         if (0 != strcmp(pEle->Value(), strNodeName))    
  24.         {    
  25.             GetNodePointerByName(pEle,strNodeName,destNode);    
  26.         }    
  27.         else    
  28.         {    
  29.             destNode = pEle;    
  30.             printf("destination node name: %s\n", pEle->Value());    
  31.             return true;    
  32.         }    
  33.     }      
  34.     return false;    
  35. }    
  36. int readXmlFile()  
  37. {  
  38.     TiXmlDocument *mydoc=new TiXmlDocument("D:\\我的资料库\\Documents\\Visual Studio 2008\\Projects\\tinyxml\\tinyxml\\RegExWithModes.xml");  
  39.     bool loadOk=mydoc->LoadFile();  
  40.     if(!loadOk)  
  41.     {  
  42.         cout<<"could not load the test file.Error:"<<mydoc->ErrorDesc()<<endl;  
  43.         exit(1);  
  44.     }  
  45.   
  46.     TiXmlElement *RootElement=mydoc->RootElement();      //根元素  
  47.     TiXmlElement *pEle=NULL;  
  48.   
  49.     GetNodePointerByName(RootElement,"SearchModes",pEle);   //找到值为SearchModes的结点  
  50.   
  51.     //遍历该结点  
  52.     for(TiXmlElement *SearchModeElement=pEle->FirstChildElement();SearchModeElement;SearchModeElement=SearchModeElement->NextSiblingElement())  
  53.     {  
  54.         cout<<SearchModeElement->Value()<<" ";  
  55.         TiXmlAttribute *pAttr=SearchModeElement->FirstAttribute();  
  56.         while (pAttr) //输出所有属性  
  57.         {  
  58.             cout<<pAttr->Name()<<":"<<pAttr->Value()<<" ";  
  59.             pAttr=pAttr->Next();  
  60.         }  
  61.         cout<<endl;  
  62.         {  
  63.             for(TiXmlElement *RegExElement=SearchModeElement->FirstChildElement();RegExElement;RegExElement=RegExElement->NextSiblingElement())//输出子元素的值  
  64.                 cout<<RegExElement->FirstChild()->Value()<<endl;  
  65.         }     
  66.     }  
  67.     delete mydoc;  
  68.     return 1;  
  69. }  
  70. int writeXmlFile()  
  71. {  
  72.     TiXmlDocument *writeDoc=new TiXmlDocument;  
  73.     TiXmlDeclaration *decl=new TiXmlDeclaration("1.0","UTF-8","yes");  
  74.     writeDoc->LinkEndChild(decl);  
  75.   
  76.     int n;      //SearchMode number;  
  77.     cout<<"Please input the number of Search Mode:";  
  78.     cin>>n;  
  79.   
  80.     TiXmlElement *RootElement=new TiXmlElement("SearchModes");  
  81.     RootElement->SetAttribute("Count",n);  
  82.     writeDoc->LinkEndChild(RootElement);  
  83.     //char *SearchMode[];  
  84.     for (int i=0;i<n;i++)  
  85.     {  
  86.         TiXmlElement *SearchModeElement=new TiXmlElement("SearchMode");  
  87.         SearchModeElement->SetAttribute("Name","Numbers");  
  88.         SearchModeElement->SetAttribute("Count",4);  
  89.         SearchModeElement->SetAttribute("Space",10000);  
  90.         RootElement->LinkEndChild(SearchModeElement);  
  91.         for(int j=0;j<4;j++)  
  92.         {  
  93.             TiXmlElement *RegExElement=new TiXmlElement("RE");  
  94.             SearchModeElement->LinkEndChild(RegExElement);  
  95.   
  96.             TiXmlText *RegExContent=new TiXmlText("[0-9]{2,2}");  
  97.             RegExElement->LinkEndChild(RegExContent);  
  98.         }  
  99.     }  
  100.     writeDoc->SaveFile("RegExWithModes.xml");  
  101.     delete writeDoc;  
  102.     return 1;  
  103. }  
  104. int main(int argc,char *argv[])  
  105. {  
  106.     readXmlFile();  
  107.     //writeXmlFile();  
  108. }  

对于XML文档为:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>  
  2. <SearchMode ModeCount="3">  
  3.     <Mode1 Name="Numbers" RECount="2" Space="110">  
  4.         <RE1>[0-9]{1,1}</RE1>  
  5.         <RE2>[0-9]{2,2}</RE2>  
  6.     </Mode1>  
  7.     <Mode2 Name="Lower_Case" RECount="3" Space="12356845">  
  8.         <RE1>[a-z]{3,3}</RE1>  
  9.         <RE2>[a-z]{4,4}</RE2>  
  10.         <RE3>[a-z]{5,5}</RE3>  
  11.     </Mode2>  
  12.     <Mode3 Name="Lower_Case_Num" RECount="4" Space="554433442">  
  13.         <RE1>[a-z0-9]{6,6}</RE1>  
  14.         <RE2>[a-z0-9]{7,7}</RE2>  
  15.         <RE3>[a-z0-9]{8,8}</RE3>  
  16.         <RE4>[a-z0-9]{9,9}</RE4>  
  17.     </Mode3>  
  18. </SearchMode>  



0 0
原创粉丝点击