最近在做毕业论文,可能要使用TinyXML。全是英文看着费劲。所以翻译了一下! 所有翻译后的文件直接被放到网盘中了。如果您需要的话自己去找吧 !网盘地址见我的主页。
网盘中有一个名字叫做《TinyXML文档集合》的word文件。
(1)http://www.grinninglizard.com/tinyxmldocs/index.html 翻译了这个页面上的所有东西
(2)http://www.grinninglizard.com/tinyxmldocs/tutorial0.html翻译了这个页面上的所有东西
TinyXML 基本操作如下,经过VS2013调试,全部能够运行!
- #include "iostream"
- #include "vector"
- #include "string"
- #include "list"
- #include "deque"
- #include "utility"
- #include "map"
- #include "set"
- #include "fstream"
- #include "sstream"
- #include "algorithm"
- #include "numeric"
- #include "iterator"
- #include "functional"
- #include "typeinfo.h"
- #include "memory"
- #include "exception"
- #include "assert.h"
- #include <bitset>
- #include "stdio.h"
- #include "stdlib.h"
- #include "malloc.h"
- #include <string.h>
- using namespace std;
- #include <stdio.h>
- #include <stdlib.h>
- #include "tinyxml.h"
- #include "tchar.h"
- using namespace std;
- // <?xml version="1.0" ?>
- // <MyApp>
- // <Messages>
- // <Welcome>Welcome to MyApp</Welcome>
- // <Farewell>Thank you for using MyApp</Farewell>
- // </Messages>
- // <Windows>
- // <Window name="MainFrame" x="5" y="15" w="400" h="250" />
- // </Windows>
- // <Connection ip="192.168.0.1" timeout="123.456000" />
- // </MyApp>
- //创建了一个D:\\yang.xml的文件
- //通过指定名字创建一个XML文件。参数是文件的名字。
- bool CreateXMLFile(char * FileName)
- {
- TiXmlDocument * pDoc = new TiXmlDocument;//创建一个文档类型对象,整个XML文档的最上层节点
- if (NULL==pDoc)
- {
- return false;
- }
- //构造函数
- // TiXmlDeclaration(const char* _version,// const char* _encoding,// const char* _standalone); standalone脱机,单独,独立
- //创造一个声明的类。<?xml version="1.0" ?>
- TiXmlDeclaration * pDeclaration = new TiXmlDeclaration(("1.0"),(""),(""));
- if (NULL==pDeclaration)
- {
- return false;
- }
- pDoc->LinkEndChild(pDeclaration);//把pDeclaration做为pDoc的一个孩子节点。
- //生成一个根节点:MyApp
- TiXmlElement * pRootEle = new TiXmlElement(("MyApp"));
- if (NULL==pRootEle)
- {
- return false;
- }
- pDoc->LinkEndChild(pRootEle);
- //生成子节点:Messages
- TiXmlElement * pMsg = new TiXmlElement(("Messages"));
- if (NULL==pMsg)
- {
- return false;
- }
- pRootEle->LinkEndChild(pMsg);
- //生成子节点welcome
- TiXmlElement * pWelcome = new TiXmlElement(("Welcome"));
- if (NULL==pWelcome)
- {
- return false;
- }
- pMsg->LinkEndChild(pWelcome);
- //设置Welcome节点的值
- const char * strValue = ("Welcome to MyApp");
- TiXmlText * pWelcomeValue = new TiXmlText(strValue);
- pWelcome->LinkEndChild(pWelcomeValue);
- //生成子节点:Farewell
- TiXmlElement * pFarewell = new TiXmlElement(("Farewell"));
- if (NULL==pFarewell)
- {
- return false;
- }
- pMsg->LinkEndChild(pFarewell);
- //设置Farewell节点的值
- strValue = ("Thank you for using MyApp");
- TiXmlText * pFarewellValue = new TiXmlText(strValue);
- pFarewell->LinkEndChild(pFarewellValue);
- //生成子节点:Windows
- 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(("name"),("MainFrame"));
- pWindow->SetAttribute(("x"),("5"));
- pWindow->SetAttribute(("y"),("15"));
- pWindow->SetAttribute(("w"),("400"));
- pWindow->SetAttribute(("h"),("250"));
- //生成子节点connection
- TiXmlElement * pConnect = new TiXmlElement(("Connection"));
- if (NULL==pConnect)
- {
- return false;
- }
- pRootEle->LinkEndChild(pConnect);
- //设置节点connection的值
- pConnect->SetAttribute(("ip"),("192.168.0.1"));
- pConnect->SetAttribute(("timeout"),("123.456000"));
- pDoc->SaveFile(FileName);
- }
- //打印整个XML文件中的所有行
- bool PintXML(const char * FileName)
- {
- TiXmlDocument * pDocument = new TiXmlDocument;
- if (NULL==pDocument)
- {
- return false;
- }
- pDocument->LoadFile(FileName);
- pDocument->Print();
- }
- //获取指向指定名字节点的指针
- //三个参数的意思:第一个参数表示一个XML文档中树根节点,strNodeName表示你要查找的节点的名字,第三个参数返回指向找到节点的指针
- bool GetNodePointerByName(TiXmlElement * pRootEle,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;
- }
- //通过名字查询一个节点中的文本内容。
- //第一个参数是XML文件名字,第二个参数是节点的名字,第三个参数是返回的节点的内容
- bool QueryNode_Text(string XmlFile,string strNodeName,string & strText)
- {
- //定义一个
- TiXmlDocument * pDoc = new TiXmlDocument;
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile.c_str());
- TiXmlElement * pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement * pNode = NULL;
- GetNodePointerByName(pRootEle,pNode);
- if (NULL!=pNode)
- {
- if (NULL!=pNode->GetText())
- {
- strText=pNode->GetText();
- return true;
- }
- else
- {
- cout << "这个节点没有text内容" << endl;
- return false;
- }
- //strText=pNode->GetText();
- //注意上面的这条语句,如果这个节点中是没有任何text内容的话,那么就会产生运行时候错误。
- //所以如果不想产生运行时错误,那么就要进行一个判断是否为空串。
- }
- else
- {
- return false;
- }
- }
- //查询节点属性输入文件名字,节点名字,和一个map。其中最后一个参数map用来保存,若干属性的值。
- bool QueryNode_Attribute(string XmlFile,map<string,string> & AttMap)
- {
- typedef pair<string,string> String_Pair;
- TiXmlDocument * pDoc = new TiXmlDocument;//定义一个文档类型
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile.c_str());
- TiXmlElement * pRootEle = pDoc->RootElement();//获取文档的根
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement * pNode = NULL;
- GetNodePointerByName(pRootEle,pNode);
- if (NULL!=pNode)
- {
- TiXmlAttribute * pAttr = NULL;//创建一个属性类
- for (pAttr = pNode->FirstAttribute(); pAttr;pAttr=pAttr->Next())//获取【每一个】属性
- {
- string strAttName = pAttr->Name();//获取属性的-----键
- string strAttValue = pAttr->Value();//获取属性的-----值
- AttMap.insert(String_Pair(strAttName,strAttValue));
- //把属性的(键---值)对做成String_Pair,然后把String_Pair对插入到Map中,并通过第三个参数的引用返回。
- }
- }
- else
- {
- return false;
- }
- return true;
- }
- //通过节点的名字,在XML文档中,删除指定的节点。注意删除一个节点,不仅仅删除了这个节点,而且会删除它的子孙。
- bool DelNode(string XmlFile,string strNodeName)
- {
- TiXmlDocument * pDoc = new TiXmlDocument;
- if (NULL == pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile.c_str());
- TiXmlElement * pRootEle = pDoc->RootElement();
- if (NULL == pRootEle)
- {
- return false;
- }
- TiXmlElement * pNode = NULL;
- GetNodePointerByName(pRootEle,pNode);
- if (pRootEle == pNode)
- {
- if (pDoc->RemoveChild(pRootEle))//如果查询的节点是根节点,那么就删除这个节点
- {
- pDoc->SaveFile(XmlFile.c_str());//删除这个节点之后保存这个新的XML文件
- return true;
- }
- else
- {
- return false;
- }
- }//endif
- if (NULL != pNode)
- {
- TiXmlNode * pParNode = pNode->Parent();//找到要删除节点的父节点
- if (NULL == pParNode)
- {
- return false;
- }
- TiXmlElement * pParentEle = pParNode->ToElement();//把一个Node类型节点转换成为一个Element类型节点
- //也就是把一个TiXmlNode类型的节点通过ToElement()调用转换成为TiXmlElement类型节点。
- if (NULL != pParentEle)
- {
- if (pParentEle->RemoveChild(pNode))//调用父节点pParentEle的一个函数RemoveChild,然后删除自己的一个孩子节点pNode
- {
- pDoc->SaveFile(XmlFile.c_str());
- return true;
- }
- else
- {
- return false;
- }
- }
- }//endif
- else
- {
- return false;
- }
- }
- //修改节点Text内容的操作
- //第一个参数是文件名字,第二个参数是要修改的节点的名字,第三个是修改的新文本
- bool ModifyNode_Text(string XmlFile,string strText)
- {
- TiXmlDocument * pDoc = new TiXmlDocument;
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile.c_str());
- TiXmlElement * pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement * pNode = NULL;
- GetNodePointerByName(pRootEle,pNode);
- if (NULL!=pNode)
- {
- pNode->Clear();//首先清除pNode指针所指向的节点原来的所有文本。
- //然后插入文本保存文件
- TiXmlText * pValue = new TiXmlText(strText.c_str());
- pNode->LinkEndChild(pValue);//创建一个TiXmlText类型的节点,让这个节点作为pNode节点的最后一个孩子。
- pDoc->SaveFile(XmlFile.c_str());//修改之后保存整个XML文件。
- return true;
- }
- else
- {
- return false;
- }
- }
- //通过指定的节点的名字,修改节点的属性值
- //第一个参数XML文件的名字,第二个参数节点的名字,第三个参数要修改的属性的值
- bool ModifyNode_Attribute(string XmlFile,string> & AttMap)
- {
- typedef pair<string,string> String_Pair;
- TiXmlDocument * pDoc = new TiXmlDocument;
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile.c_str());
- TiXmlElement * pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement * pNode = NULL;
- GetNodePointerByName(pRootEle,pNode);
- if (NULL!=pNode)
- {
- TiXmlAttribute * pAttr = NULL;
- string strAttName = ("");//原来这里的代码上是这样的 _T("");但是这样的话编译的时候就会出现错误。
- string strAttValue = ("");
- for (pAttr = pNode->FirstAttribute(); pAttr;pAttr=pAttr->Next())
- {
- strAttName = pAttr->Name();
- map<string,string>::iterator iter;
- for (iter = AttMap.begin(); iter != AttMap.end();iter++)
- {//遍历整个的属性map,如果找到和输入的属性名字相同的属性,那么就把指定的属性值设置成为输入属性的值。
- if (strAttName==iter->first)
- {
- pAttr->SetValue((iter->second).c_str());
- }
- }
- }
- pDoc->SaveFile(XmlFile.c_str());
- return true;
- }
- else
- {
- return false;
- }
- }
- //在指定的节点下面增加一个孩子节点,并且这个新节点可以设置text内容
- //第一个参数是XML文档的文件名字,第二个参数是要插入的父节点的名字,第三个参数是新创建的节点的名字,第四个参数是新创建的节点的文本内容
- bool AddNode_Text(string XmlFile,string strParNodeName,strParNodeName,pNode);//首先找到父节点,因为将在这个节点下面插入新创建的子节点
- if (NULL!=pNode)
- {
- //生成子节点
- TiXmlElement * pNewNode = new TiXmlElement(strNodeName.c_str());
- if (NULL==pNewNode)
- {
- return false;
- }
- //设置节点文本,然后插入节点
- TiXmlText * pNewValue = new TiXmlText(strText.c_str());
- pNewNode->LinkEndChild(pNewValue);//TiXmlNode* LinkEndChild( TiXmlNode* addThis )
- pNode->InsertEndChild(*pNewNode);//TiXmlNode* InsertEndChild( const TiXmlNode& addThis ),这个函数的实现调用了InsertEndChild()
- pDoc->SaveFile(XmlFile.c_str());
- return true;
- }
- else
- {
- return false;
- }
- }
- //在指定名字节点下面增加一个节点,并且设置这个节点的属性值
- //第一个参数是要操作的XML文件的名字,第二个参数是父节点的名字,第三个参数是新插入的节点的名字,第四个参数是要设置的属性值
- bool AddNode_Attribute(string XmlFile,string>&AttMap)
- {
- TiXmlDocument * pDoc = new TiXmlDocument;
- if (NULL == pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile.c_str());
- TiXmlElement * pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement * pNode = NULL;
- GetNodePointerByName(pRootEle,pNode);
- if (NULL!=pNode)
- {
- TiXmlElement * pNewNode = new TiXmlElement(strNodeName.c_str());
- if (NULL==pNewNode)
- {
- return false;
- }
- map<string,string>::iterator iter;
- for (iter = AttMap.begin(); iter != AttMap.end();iter++)
- {
- pNewNode->SetAttribute(iter->first.c_str(),iter->second.c_str());
- }
- pNode->InsertEndChild(*pNewNode);
- pDoc->SaveFile(XmlFile.c_str());
- return true;
- }
- else
- {
- return false;
- }
- }
- //打印出整个的XML文档
- bool PintXML(string FileName)
- {
- TiXmlDocument *pDoc = new TiXmlDocument();
- if (NULL == pDoc)
- {
- return false;
- }
- pDoc->LoadFile(FileName.c_str());
- pDoc->Print();
- getchar();
- }
- int main(int argc,char * argv[])
- {
- char * filename = "D:\\yang.xml";
- {//块1
- //创建一个yang.xml文件
- bool result = CreateXMLFile(filename);
- if (!result)
- {
- cout << "出现了错误!" << endl;
- }
- else
- {
- cout << "XML文件创建成功" << endl << endl;
- }
- //打印整个的yang.xml文件
- {
- PintXML(filename);
- }
- }//块1结束
- {//块2
- //在这个yang.xml文件中查找welcome节点,并获取一个指向这个节点的指针,然后输出这个节点的类型。
- TiXmlDocument * pDocument = new TiXmlDocument;
- if (NULL == pDocument)
- {
- return false;
- }
- pDocument->LoadFile(filename);
- TiXmlElement * pRoot = pDocument->RootElement();
- string strNodeName("Window");
- TiXmlElement * pToNode = NULL;
- GetNodePointerByName(pRoot,pToNode);
- if (pToNode)
- {
- cout << "\n========================" << endl;
- cout << pToNode->Type() << endl;//这里会打印类型1,因为这是一个TiXmlElement类型的对象。
- cout << "========================" << endl;
- }
- // enum NodeType
- // {
- // TINYXML_DOCUMENT,0
- // TINYXML_ELEMENT,1
- // TINYXML_COMMENT,2
- // TINYXML_UNKNOWN,3
- // TINYXML_TEXT,4
- // TINYXML_DECLARATION,5
- // TINYXML_TYPECOUNT 6
- // };
- }//块2结束
- {//块3
- //查找指定名字的节点的内容,然后输出
- {
- //查询一个节点中的文本内容,[此处查找的Windows节点是没有text内容的,查找不成功]
- string strOutput;
- string FileName(filename);
- string strNodeName("Window");
- bool result = QueryNode_Text(FileName,strOutput);
- if (result)
- {
- cout << "\n========================" << endl;
- cout << strOutput << endl;
- cout << "========================" << endl;
- }
- }
- {
- //查询一个节点中的文本内容,[此处查找的Welcome节点是有text内容的,查找成功]
- string strOutput;
- string FileName(filename);
- string strNodeName("Welcome");
- bool result = QueryNode_Text(FileName,strOutput);
- if (result)
- {
- cout << "\n========================" << endl;
- cout << strOutput << endl;
- cout << "========================" << endl;
- }
- }
- }//块3结束
- {//块4开始
- //通过节点名字查找节点的所有属性。
- map<string,string> AttMap_inPut;
- map<string,string>::iterator iter;
- string FileName(filename);
- string strNodeName("Window");
- bool result = QueryNode_Attribute(FileName,AttMap_inPut);
- if (result)
- {
- for (iter = AttMap_inPut.begin(); iter != AttMap_inPut.end(); ++iter)
- {
- cout << iter->first << "\t" << iter->second << endl;
- }
- cout << "========================" << endl;
- }
- else
- {
- cout << "读取属性出现错误" << endl;
- }
- }//块4结束
- {//块5开始
- //通过节点的名字,在XML文档中,删除指定的节点。
- getchar();
- string FileName(filename);
- string strNodeName("Welcome");
- bool result = DelNode(FileName,strNodeName);
- if (false == result)
- {
- cout << "删除节点错误!\n" << endl;
- }
- else
- {
- cout << "删除节点成功!\n" << endl;
- }
- }//块5结束
- {//块6开始
- //修改指定名字节点的text值。
- getchar();
- string FileName(filename);
- string strNodeName("Farewell");
- string strText("This is the lastest text");
- bool result = ModifyNode_Text(FileName,strText);
- if (!result)
- {
- cout << "\n修改节点文本信息错误!" << endl;
- }
- else
- {
- cout << "\n修改名字为:" << strNodeName << "节点信息为" << strText << endl;
- cout << "修改成功!\n" << endl;
- }
- }//块6结束
- {//块7开始
- //修改指定名字节点的属性的值
- map<string,string> AttMap_input;
- //name="MainFrame" x="5" y="15" w="400" h="250"
- AttMap_input.insert(pair<string,string>("name","_MainFrame"));
- AttMap_input.insert(pair<string,string>("x","_5"));
- AttMap_input.insert(pair<string,string>("y","_15"));
- AttMap_input.insert(pair<string,string>("w","_400"));
- AttMap_input.insert(pair<string,string>("h","_250"));
- string FileName(filename);
- string strNodeName("Window");
- bool result = ModifyNode_Attribute(FileName,AttMap_input);
- cout << result << endl;
- if (!result)
- {
- cout << "改变属性值操作失败!" << endl;
- }
- else
- {
- cout << "改变属性值操作成功!" << endl;
- }
- }//块7结束
- {//块8开始
- //在指定名字的节点下面创建一个新的节点。并且设置新的节点的text内容。
- string FileName(filename);
- string strParNodeName("Windows");
- string strNodeName("Window_new");
- string strText("This is a new Create Node!");
- bool result = AddNode_Text(FileName,strText);
- if (result)
- {
- cout << "创建指定名字:" << strNodeName << "的新节点成功!" << endl;
- }
- else
- {
- cout << "创建节点失败" << endl;
- }
- }//块8结束
- {//块9开始
- //插入新的节点并设置节点的属性值。
- map<string,string>("_name",string>("_x",string>("_y",string>("_w",string>("_h","_250"));
- string FileName(filename);
- string strParNodeName("MyApp");
- string strNodeName("NewAtt");
- bool result = AddNode_Attribute(FileName,AttMap_input);
- if (result)
- {
- cout << "增加新的节点,并设置属性值成功" << endl;
- }
- else
- {
- cout << "增加新的节点,并设置属性值失败" << endl;
- }
- }//块9结束
- {//块10,打印出整个的XML文档
- string FileName(filename);
- PintXML(FileName);
- }
- getchar();
- return 0;
- }