TinyXML代码整合成为类

我将所有的TinyXML代码封装到一个类中,

#pragma once
#ifdef TIXML_USE_STL
#include <iostream>
#include <map>
#include <string>
using namespace std;
#else
#include <stdio.h>
#endif
#include "tinyxml.h"
#include "tinystr.h"

class CHandleXML
{
public:
	CHandleXML();
	virtual ~CHandleXML();
	//创建一个XML文件
	bool virtual createXML()=0;
	//打印出XML
	bool PaintXml();//调试用 给出XML的名
	//获得XML的Declare
	bool GetXmlDeclare();//给出XMLFILE名字
	//根据节点名字获取节点指针  查询节点的Text
	bool GetNodePointerByName(TiXmlElement* pRootEle,std::string& strNodeName,TiXmlElement* &Node);
	//根据节点的名字,查询文本  //给出strNodeName,返回strText
	bool QueryNode_Text();
	//查询指定节点的属性
//给出文件名字,strNodeName 要查询的节点名字 返回:AttMap要查询的属性名字 
	bool QueryNode_Attribute();
	//删除指定的节点的值  //in:XMLFILE名字,strNode的删除节点的名字,
	bool DelNode();
	//修改指定节点的文本 in XmlFile,strNodeName,strText
	bool ModifyNode_Text();
	//修改指定节点的属性值 in: XmlFile name,strNodeName 修改的节点,修改的属性值集合
	bool ModifyNode_Attribute();
	//增加指定节点为文本 strParNodeName:要增加的节点的父节点 strText:增加的文本 XMLFIle名字,strNodeName:新增加节点的名字,strText新增加节点的文本
	bool AddNode_Text();
	//增加指定节点的属性值  xmlfile名字,strParNodeName 要增加节点的父节点,strNodeName新增加节点名字,AttMap新增的属性列表
	bool AddNode_Attribute();
public:
	std::string XmlFile;//XML文件路径
	std::string strNodeName;//当前节点的名字
	std::string strParNodeName;//当前节点父节点的名字
	std::map<std::string,std::string> AttMap;//节点包含的属性map
	std::string strText;//文本
	TiXmlElement *pRootEle;//根节点
	TiXmlElement* Node;
	std::string strVersion;
	std::string strStandalone;
	std::string strEncoding;
};


 

#include "stdafx.h"
#include "HandleXml.h"
CHandleXML::CHandleXML()
{
	pRootEle=NULL;
	Node=NULL;
}
CHandleXML::~CHandleXML()
{
	if(pRootEle)
		delete pRootEle;
	if(Node)
		delete Node;
}
bool CHandleXML::PaintXml()
{
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	pDoc->Print();
	return true;
}
bool CHandleXML::GetXmlDeclare()
{
	//定义一个TiXmlDocument类指针
	TiXmlDocument *pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlNode* pXmlFirst=pDoc->FirstChild();
	if(NULL!=pXmlFirst)
	{
		TiXmlDeclaration* pXmlDec=pXmlFirst->ToDeclaration();
		if(NULL!=pXmlDec)
		{
			strVersion=pXmlDec->Version();
			strStandalone=pXmlDec->Standalone();
			strEncoding=pXmlDec->Encoding();
		}
	}
}
bool CHandleXML::GetNodePointerByName(TiXmlElement* pRootEle,std::string& strNodeName,TiXmlElement* &Node)
{
	//假如等于根节点名字,则返回
	if(strNodeName==pRootEle->Value())
	{
		Node=pRootEle;
		return true;
	}
	TiXmlElement* pEle=pRootEle;
	for(pEle=pRootEle->FirstChildElement();pEle;pEle=pEle->NextSiblingElement())
	{
		//递归处理子节点 获取节点指针
		if(GetNodePointerByName(pEle,strNodeName,Node))
			return true;
	}
	return false;
}
//给出strNodeName,返回strText
bool CHandleXML::QueryNode_Text()
{
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement *pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement* pNode=NULL;
	GetNodePointerByName(pRootEle,strNodeName,pNode);
	if(NULL!=pNode)
	{
		strText=pNode->GetText();
		return true;
	}
	else
	{
		return false;
	}
}
bool CHandleXML::QueryNode_Attribute()
{
	typedef std::pair<std::string,std::string> String_Pair;
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement *pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement* pNode=NULL;
	GetNodePointerByName(pRootEle,strNodeName,pNode);
	if(NULL!=pNode)
	{
		TiXmlAttribute* pAttr=NULL;
		for(pAttr=pNode->FirstAttribute();pAttr;pAttr=pAttr->Next())
		{
			std::string strAttName=pAttr->Name();
			std::string strAttValue=pAttr->Value();
			AttMap.insert(String_Pair(strAttName,strAttValue));
		}
		return true;
	}
	else
		return false;
	return false;
}
//in:XMLFILE名字,strNode的删除节点的名字,
bool CHandleXML::DelNode()
{
	TiXmlDocument *pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement* pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement* pNode=NULL;
	GetNodePointerByName(pRootEle,strNodeName,pNode);
	if(pRootEle==pNode)
	{
		if(pDoc->RemoveChild(pRootEle))
		{
			pDoc->SaveFile(XmlFile);
			return true;
		}
		else 
			return false;
	}
	//加入是其他节点
	if(NULL!=pNode)
	{
		TiXmlNode* pParNode=pNode->Parent();
		if(NULL==pParNode)
			return false;
		TiXmlElement* pParentEle=pParNode->ToElement();
		if(NULL!=pParentEle)
		{
			if(pParentEle->RemoveChild(pNode))
				pDoc->SaveFile(XmlFile);
			else
				return false;
		}
	}
	else
	{
		return false;
	}
	return false;
}
bool CHandleXML::ModifyNode_Text()
{
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement* pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement* pNode=NULL;
	GetNodePointerByName(pRootEle,strNodeName,pNode);
	if(NULL!=pNode)
	{
		pNode->Clear();//首先清除所有的文本
		TiXmlText* pValue=new TiXmlText(strText);
		pNode->LinkEndChild(pValue);
		pDoc->SaveFile(XmlFile);
		return true;
	}
	else
		return false;
	return false;
}
bool CHandleXML::ModifyNode_Attribute()
{
	typedef std::pair<std::string,std::string> String_Pair;
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement* pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement* pNode;
	GetNodePointerByName(pRootEle,strNodeName,pNode);
	if(NULL!=pNode)
	{
		TiXmlAttribute* pAttr=NULL;
		std::string strAttName="";
		std::string strAttValue="";
		for(pAttr=pNode->FirstAttribute();pAttr;pAttr=pAttr->Next())
		{
			strAttName=pAttr->Name();
			std::map<std::string,std::string>::iterator iter;
			for(iter=AttMap.begin();iter!=AttMap.end();iter++)
			{
				if(strAttName==iter->first)
				{
					pAttr->SetValue(iter->second);
				}
			}
		}
		pDoc->SaveFile(XmlFile);
		return true;
	}
	else
		return false;
	return false;
}
bool CHandleXML::AddNode_Text()
{
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement* pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement *pNode=NULL;
	GetNodePointerByName(pRootEle,strParNodeName,pNode);
	if(NULL!=pNode)
	{
		//申请子节点 pNewNode
		TiXmlElement* pNewNode=new TiXmlElement(strNodeName);
		if(NULL==pNewNode)
			return false;
		//设置节点文本 然后插入及诶单
		TiXmlText* pNewValue=new TiXmlText(strText);
		pNewNode->LinkEndChild(pNewValue);
		pNode->InsertEndChild(*pNewNode);
		pDoc->SaveFile(XmlFile);
		return true;


	}
	else
		return false;
	return false;
}
bool CHandleXML::AddNode_Attribute()
{
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	pDoc->LoadFile(XmlFile);
	TiXmlElement* pRootEle=pDoc->RootElement();
	if(NULL==pRootEle)
		return false;
	TiXmlElement *pNode=NULL;
	GetNodePointerByName(pRootEle,strParNodeName,pNode);
	if(NULL!=pNode)
	{
		//申请子节点 pNewNode
		TiXmlElement* pNewNode=new TiXmlElement(strNodeName);
		if(NULL==pNewNode)
			return false;
		//设置节点的属性值,然后插入节点
		std::map<std::string,std::string>::iterator iter;
		for(iter=AttMap.begin();iter!=AttMap.end();iter++)
		{
			pNewNode->SetAttribute(iter->first,iter->second);
		}
		pNode->InsertEndChild(*pNewNode);
		pDoc->SaveFile(XmlFile);
		return true;
	}
	else
	{
		return false;
	}
}


 

#pragma once
#include "HandleXML.h"
class CMyXML:public CHandleXML
{
public:
	CMyXML(){}
	virtual ~CMyXML(){}
	bool virtual createXML();
};


 

bool CMyXML::createXML()
{
	TiXmlDocument* pDoc=new TiXmlDocument();
	if(NULL==pDoc)
		return false;
	
	TiXmlDeclaration* pDeclaration=new TiXmlDeclaration("1.0","","");
	if(NULL==pDeclaration)
		return false;
	pDoc->LinkEndChild(pDeclaration);

	//生成一个根节点:MyApp
	TiXmlElement* pRootEle=new TiXmlElement("MyApp");
	if(NULL==pRootEle)
	{
		return false;
	}
	pDoc->LinkEndChild(pRootEle);
	TiXmlElement* pMsg=new TiXmlElement("Messages");
	if(NULL==pMsg)
		return false;
	pRootEle->LinkEndChild(pMsg);
	TiXmlElement* pWelcome=new TiXmlElement("Welcome");
	if(NULL==pWelcome)
		return false;
	pMsg->LinkEndChild(pWelcome);
	std::string strValue="Welcome to MyApp";
	TiXmlText* pWelcomeValue=new TiXmlText(strValue);
	pWelcome->LinkEndChild(pWelcomeValue);
	
	TiXmlElement* pFarewell=new TiXmlElement("Farewell");
	if(NULL==pFarewell)
		return false;
	pMsg->LinkEndChild(pFarewell);
	strValue="Thank you for using myApp";
	TiXmlText* pFarewellValue=new TiXmlText(strValue);
	pFarewell->LinkEndChild(pFarewellValue);

	TiXmlElement* pTest0=new TiXmlElement("MyTest0");
	if(NULL==pTest0)
		return false;
	pMsg->LinkEndChild(pTest0);
	strValue="MyTestddd";
	TiXmlText* pTest0Value=new TiXmlText(strValue);
	pTest0->LinkEndChild(pTest0Value);
	//生成子节点widnows
	TiXmlElement* pWindows=new TiXmlElement("Windows");
	if(NULL==pWindows)
		return false;
	pRootEle->LinkEndChild(pWindows);
	//生成子节点window
	TiXmlElement* pWindow=new TiXmlElement("Window");
	if(NULL==pWindow)
		return false;
	pWindows->LinkEndChild(pWindow);
	//设置window的属性值
	pWindow->SetAttribute("hello","MainFrame");
	pWindow->SetAttribute("x","5");
	pWindow->SetAttribute("y","15");
	pWindow->SetAttribute("w","400");
	pWindow->SetAttribute("h","250");
	//
	TiXmlElement* pConnection=new TiXmlElement("Connection");
	if(NULL==pConnection)
		return false;
	pRootEle->LinkEndChild(pConnection);
	pConnection->SetAttribute("name","miaolinjie");
	pConnection->SetAttribute("sex","boy");
	pDoc->SaveFile(XmlFile);
	return true;
}


 

int main()
{
	CMyXML testXML;
	testXML.XmlFile="sts.xml";
	testXML.createXML();
	testXML.PaintXml();
	testXML.GetXmlDeclare();
	testXML.strNodeName="Connection";
	testXML.QueryNode_Attribute();
	cout<<testXML.AttMap.begin()->second.c_str()<<endl;
	testXML.strNodeName="MyTest0";
	testXML.strText="fff";
	testXML.ModifyNode_Text();
	testXML.AttMap.clear();
	testXML.AttMap.insert(std::pair<std::string,std::string>("name","ddddd"));
	testXML.strNodeName="Connection";
	testXML.ModifyNode_Attribute();
	testXML.PaintXml();
	testXML.strNodeName="MyHOo";
	testXML.strParNodeName="Messages";
	testXML.strText="d";
	testXML.AddNode_Text();
	testXML.PaintXml();
	testXML.AttMap.clear();
	testXML.AttMap.insert(std::pair<std::string,std::string>("dddd","f"));
	testXML.AddNode_Attribute();
	testXML.strText="fffffff";
	testXML.ModifyNode_Text();
	testXML.PaintXml();
	//std::string mytest;
	//QueryNode_Text(str,"Farewell",mytest);
	//cout<<mytest.c_str()<<endl;
	//std::map<std::string,std::string> AttMap;
	//QueryNode_Attribute(str,"Connection",AttMap);
	//std::map<std::string,std::string>::iterator iter=AttMap.begin();
	for(;iter!=AttMap.end();iter++)
	{
		iter->second="ddss";
		cout<<iter->first.c_str()<<" "<<iter->second.c_str()<<endl;
	}
	//if(iter!=AttMap.end())
	//	iter->second="helloworld";
	DelNode(str,"Farewell");
	//ModifyNode_Text(str,"Farewell","cc");
	//ModifyNode_Attribute(str,"Connection",AttMap);
	//AddNode_Text(str,"Messages","zengjia","1111");
	//std::map<std::string,std::string> test;
	//
		AttMap.insert(String_Pair(strAttName,strAttValue));
	//test.insert(std::pair<std::string,std::string>("name","d"));
	//AddNode_Attribute(str,"Messages","zenddgjia",test);
	//PaintXml(str);
	return 0;
}


 

tinyxpath 解析简单 的小工具,输出是一个静态库。可 找到xml文档. TinyXml介绍 TinyXml是一个基于DOM模型的、非验证的轻量级C++解释器 一. XML解析模型: 目前XML的解析主要有两大模型:SAX和DOM。 SAX是基于事件的,其基本工作流程是分析XML文档,当发现了一个新的元素时,产生一个对应事件,并调用相应的用户处理函数。这种方式占用内存少,速度快,但用户程序相应得会比较复杂。 DOM(文档对象模型),则是在分析时,一次性的将整个XML文档进行分析,并在内存中形成对应的树结构,同时,向用户提供一系列的接口来访问和编辑该树结构。这种方式占用内存大,速度往往慢于SAX,但可以给用户提供一个面向对象的访问接口,对用户更为友好。 另据说,一些同时提供了SAX和DOM接口的库,是在底层先实现SAX,再在SAX的基础上实现DOM 对于一个特定的XML文档而言,其正确性分为两个层次。 首先是其格式应该符合XML的基本格式要求,比如第一行要有声明,标签的嵌套层次必须前后一致等等,符合这些要求的文件,就是一个合格的XML文件,称作well-formatted。 其次,一个XML文档因其内容的不同还必须在语义上符合相应的标准,这些标准由相应的DTD文件或者Schema文件来定义,符合了这些定义要求的XML文件,称作valid。 因此,解析器也分为两种,一种是验证的,即会跟据XML文件中的声明,用相应的DTD文件对XML文件进行校验,检查它是否满足DTD文件的要求。另一种是忽略DTD文件,只要基本格式正确,就可以进行解析。 就我所知,验证的解析器通常都是比较重量级的。TinyXml不支持验证,但是体积很小,用在解析格式较为简单的XML文件,比如配置文件时,特别的合适。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值