我将所有的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;
}