http://blog.csdn.net/mjay1234/article/details/7380536
#include<iostream>
#include <string.h>
#include <cstdlib>
#include "tinystr.h"
#include "tinyxml.h"
using namespace std;
bool GetNodePointerByName(TiXmlElement* pRootEle, const char* strNodeName,TiXmlElement* &destNode)
{ //根据结点名获取结点指针
// 假如等于根节点名,就退出
if (0 == strcmp(strNodeName, pRootEle->Value()))
{
destNode = pRootEle;
return true;
}
TiXmlElement* pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// 递归处理子节点,获取节点指针
if (0 != strcmp(pEle->Value(), strNodeName))
{
GetNodePointerByName(pEle,strNodeName,destNode);
}
else
{
destNode = pEle;
printf("destination node name: %s\n", pEle->Value());
return true;
}
}
return false;
}
int readXmlFile()
{
TiXmlDocument *mydoc=new TiXmlDocument("D:\\我的资料库\\Documents\\Visual Studio 2008\\Projects\\tinyxml\\tinyxml\\RegExWithModes.xml");
bool loadOk=mydoc->LoadFile();
if(!loadOk)
{
cout<<"could not load the test file.Error:"<<mydoc->ErrorDesc()<<endl;
exit(1);
}
TiXmlElement *RootElement=mydoc->RootElement(); //根元素
TiXmlElement *pEle=NULL;
GetNodePointerByName(RootElement,"SearchModes",pEle); //找到值为SearchModes的结点
//遍历该结点
for(TiXmlElement *SearchModeElement=pEle->FirstChildElement();SearchModeElement;SearchModeElement=SearchModeElement->NextSiblingElement())
{
cout<<SearchModeElement->Value()<<" ";
TiXmlAttribute *pAttr=SearchModeElement->FirstAttribute();
while (pAttr) //输出所有属性
{
cout<<pAttr->Name()<<":"<<pAttr->Value()<<" ";
pAttr=pAttr->Next();
}
cout<<endl;
{
for(TiXmlElement *RegExElement=SearchModeElement->FirstChildElement();RegExElement;RegExElement=RegExElement->NextSiblingElement())//输出子元素的值
cout<<RegExElement->FirstChild()->Value()<<endl;
}
}
delete mydoc;
return 1;
}
int writeXmlFile()
{
TiXmlDocument *writeDoc=new TiXmlDocument;
TiXmlDeclaration *decl=new TiXmlDeclaration("1.0","UTF-8","yes");
writeDoc->LinkEndChild(decl);
int n; //SearchMode number;
cout<<"Please input the number of Search Mode:";
cin>>n;
TiXmlElement *RootElement=new TiXmlElement("SearchModes");
RootElement->SetAttribute("Count",n);
writeDoc->LinkEndChild(RootElement);
//char *SearchMode[];
for (int i=0;i<n;i++)
{
TiXmlElement *SearchModeElement=new TiXmlElement("SearchMode");
SearchModeElement->SetAttribute("Name","Numbers");
SearchModeElement->SetAttribute("Count",4);
SearchModeElement->SetAttribute("Space",10000);
RootElement->LinkEndChild(SearchModeElement);
for(int j=0;j<4;j++)
{
TiXmlElement *RegExElement=new TiXmlElement("RE");
SearchModeElement->LinkEndChild(RegExElement);
TiXmlText *RegExContent=new TiXmlText("[0-9]{2,2}");
RegExElement->LinkEndChild(RegExContent);
}
}
writeDoc->SaveFile("RegExWithModes.xml");
delete writeDoc;
return 1;
}
int main(int argc,char *argv[])
{
readXmlFile();
//writeXmlFile();
}
对于XML文档为:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<SearchMode ModeCount="3">
<Mode1 Name="Numbers" RECount="2" Space="110">
<RE1>[0-9]{1,1}</RE1>
<RE2>[0-9]{2,2}</RE2>
</Mode1>
<Mode2 Name="Lower_Case" RECount="3" Space="12356845">
<RE1>[a-z]{3,3}</RE1>
<RE2>[a-z]{4,4}</RE2>
<RE3>[a-z]{5,5}</RE3>
</Mode2>
<Mode3 Name="Lower_Case_Num" RECount="4" Space="554433442">
<RE1>[a-z0-9]{6,6}</RE1>
<RE2>[a-z0-9]{7,7}</RE2>
<RE3>[a-z0-9]{8,8}</RE3>
<RE4>[a-z0-9]{9,9}</RE4>
</Mode3>
</SearchMode>