TinyXml使用实例


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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值