Tinyxml's popular C++ XML parser is excellent

  • 2020-04-01 21:25:59
  • OfStack

Reading and setting up XML configuration files is the most common operation. I have tried several C++ XML parsers, and personally feel that TinyXML is the most comfortable to use, because its API interface is very similar to that of Java, and it is very object-oriented.
TinyXML is an open source parsing library that parses XML, can be used in C++, and can be compiled in Windows or Linux. The model of the parse library parses the XML file and then generates the DOM model in memory, allowing us to easily traverse the XML tree.
DOM model is the document object model, which divides the entire document into multiple elements (such as book, chapter, section, segment, etc.), and USES a tree structure to represent the sequential relationships and nested inclusion relationships among these elements.
Here is an XML fragment:

 
<Persons> 
<Person ID="1"> 
<name> Week star </name> 
<age>20</age> 
</Person> 
<Person ID="2"> 
<name> The white is clear and bright </name> 
<age>18</age> 
</Person> 
</Persons> 

In TinyXML, classes are defined based on the various elements of XML:
TiXmlBase: the base class for the entire TinyXML model.
TiXmlAttribute: an attribute that corresponds to an element in the XML.
TiXmlNode: corresponds to a node in the DOM structure.
TiXmlComment: corresponds to a comment in the XML
TiXmlDeclaration: corresponds to the declaration part in XML, that is, < ? Versiong = "1.0"? > .
TiXmlDocument: the entire document that corresponds to XML.
TiXmlElement: the element that corresponds to XML.
TiXmlText: the literal part that corresponds to the XML
TiXmlUnknown: corresponds to an unknown part of the XML.
TiXmlHandler: defines some operations for XML.
TinyXML is a parsing library consisting of DOM model classes (TiXmlBase, TiXmlNode, TiXmlAttribute, TiXmlComment, TiXmlDeclaration, TiXmlElement, TiXmlText, TiXmlUnknown) and action classes (TiXmlHandler). It consists of two header files (.h file) and four CPP files (.cpp file). When used, just import (tinyxml.h, tinystr.h, tinystr.cpp, tinyxml.cpp, tinyxmlerror.cpp, tinyxmlparser.cpp) into the project to use its stuff. If necessary, you can make it your own DLL to call. An example will illustrate everything...
Corresponding XML file:
 
<Persons> 
<Person ID="1"> 
<name>phinecos</name> 
<age>22</age> 
</Person> 
</Persons> 

Program code for reading and writing XML files:
 
#include <iostream> 
#include "tinyxml.h" 
#include "tinystr.h" 
#include <string> 
#include <windows.h> 
#include <atlstr.h> 
using namespace std; 
CString GetAppPath() 
{//Gets the application root directory
TCHAR modulePath[MAX_PATH]; 
GetModuleFileName(NULL, modulePath, MAX_PATH); 
CString strModulePath(modulePath); 
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\'))); 
return strModulePath; 
} 
bool CreateXmlFile(string& szFileName) 
{//Create an XML file, the path that szFilePath saves for the file, and return true on success, or false
try 
{ 
//Create an XML document object.
TiXmlDocument *myDocument = new TiXmlDocument(); 
//Create a root element and connect.
TiXmlElement *RootElement = new TiXmlElement("Persons"); 
myDocument->LinkEndChild(RootElement); 
//Create a Person element and connect.
TiXmlElement *PersonElement = new TiXmlElement("Person"); 
RootElement->LinkEndChild(PersonElement); 
//Sets the attributes of the Person element.
PersonElement->SetAttribute("ID", "1"); 
//Create the name element, the age element, and the connection.
TiXmlElement *NameElement = new TiXmlElement("name"); 
TiXmlElement *AgeElement = new TiXmlElement("age"); 
PersonElement->LinkEndChild(NameElement); 
PersonElement->LinkEndChild(AgeElement); 
//Sets and connects the contents of the name and age elements.
TiXmlText *NameContent = new TiXmlText(" Week star "); 
TiXmlText *AgeContent = new TiXmlText("22"); 
NameElement->LinkEndChild(NameContent); 
AgeElement->LinkEndChild(AgeContent); 
CString appPath = GetAppPath(); 
string seperator = "\"; 
string fullPath = appPath.GetBuffer(0) +seperator+szFileName; 
myDocument->SaveFile(fullPath.c_str());//Save to file
} 
catch (string& e) 
{ 
return false; 
} 
return true; 
} 
bool ReadXmlFile(string& szFileName) 
{//The Xml file is read and traversed
try 
{ 
CString appPath = GetAppPath(); 
string seperator = "\"; 
string fullPath = appPath.GetBuffer(0) +seperator+szFileName; 
//Create an XML document object.
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str()); 
myDocument->LoadFile(); 
//Gets the root element, Persons.
TiXmlElement *RootElement = myDocument->RootElement(); 
//Output the name of the root element, i.e., output Persons.
cout << RootElement->Value() << endl; 
//Get the first Person node.
TiXmlElement *FirstPerson = RootElement->FirstChildElement(); 
//Gets the name node and the age node and ID attributes of the first Person.
TiXmlElement *NameElement = FirstPerson->FirstChildElement(); 
TiXmlElement *AgeElement = NameElement->NextSiblingElement(); 
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute(); 
//Output the name content of the first Person, namely the circumstellar; Age content, that is; ID attribute, that is.
cout << NameElement->FirstChild()->Value() << endl; 
cout << AgeElement->FirstChild()->Value() << endl; 
cout << IDAttribute->Value()<< endl; 
} 
catch (string& e) 
{ 
return false; 
} 
return true; 
} 
int main() 
{ 
string fileName = "info.xml"; 
CreateXmlFile(fileName); 
ReadXmlFile(fileName); 
} 


Related articles: