Reading and setting the XML configuration file is the most common operation, try a few C + + XML parser, personal feeling tinyxml is the most comfortable to use, because its API interface and Java is very similar, object-oriented very good.
TinyXML is an open source parsing XML parsing library that can be used in C + + and can be compiled in Windows or Linux. The model of this analytic library parses the XML file and then generates the DOM model in memory, which makes it easy to traverse the XML tree.
The DOM model, the Document object model, divides the entire document into elements such as books, chapters, sections, paragraphs, and so on, and uses a tree structure to represent the order relationships between these elements and the nested containment relationships.
Here is an XML fragment:
<Persons>
<person id= "1" >
<name> Week stars </name>
<age>20</age>
</Person>
<person id= "2" >
<name> Bai Jingjing </name>
<age>18</age>
</Person>
</Persons>
In TinyXML, some classes are defined according to the various elements of XML:
Tixmlbase: The base class for the entire TinyXML model.
Tixmlattribute: A property that corresponds to an element in XML.
Tixmlnode: Corresponds to a node in the DOM structure.
Tixmlcomment: corresponding to comments in XML
Tixmldeclaration: Corresponds to the declaration part of XML, namely < Versiong= "1.0"?>.
Tixmldocument: The entire document that corresponds to XML.
Tixmlelement: The element that corresponds to the XML.
Tixmltext: The text portion corresponding to the XML
Tixmlunknown: Corresponds to an unknown part of XML.
Tixmlhandler: Defines some operations for XML.
TinyXML is an analytic library, mainly by Dom model classes (Tixmlbase, Tixmlnode, Tixmlattribute, Tixmlcomment, Tixmldeclaration, Tixmlelement, Tixmltext , Tixmlunknown) and Operation classes (Tixmlhandler). It consists of two header files (. h files) and four CPP files (. cpp files), as long as they are used (tinyxml.h, Tinystr.h, Tinystr.cpp, Tinyxml.cpp, Tinyxmlerror.cpp, TINYXMLPARSER.CPP) Import project can use it for something. If you want, you can call it by making it your own DLL. For example, you can explain everything ...
The corresponding XML file:
<Persons>
<person id= "1" >
<name>phinecos</name>
<age>22</age>
</Person>
</Persons>
program code to read and write XML files:
#include <iostream>
#include "tinyxml.h"
#include "Tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
using namespace Std;
CString Getapppath ()
{//Get 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 XML file, Szfilepath for file save path, False if create successfully return True
Try
{
Creates an XML Document object.
Tixmldocument *mydocument = new Tixmldocument ();
Create a root element and connect it.
Tixmlelement *rootelement = new Tixmlelement ("Persons");
Mydocument->linkendchild (rootelement);
Create a person element and connect.
Tixmlelement *personelement = new Tixmlelement ("person");
Rootelement->linkendchild (personelement);
Sets the properties of the person element.
Personelement->setattribute ("ID", "1");
Create a name element, an age element, and connect.
Tixmlelement *nameelement = new Tixmlelement ("name");
Tixmlelement *ageelement = new Tixmlelement ("Age");
Personelement->linkendchild (nameelement);
Personelement->linkendchild (ageelement);
Sets the contents of the name element and the age element and connects.
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)
{//Read the XML file and traverse
Try
{
CString AppPath = Getapppath ();
string seperator = "\ \";
String fullPath = Apppath.getbuffer (0) +seperator+szfilename;
Creates an XML Document object.
Tixmldocument *mydocument = new Tixmldocument (FULLPATH.C_STR ());
Mydocument->loadfile ();
Gets the root element, which is persons.
Tixmlelement *rootelement = Mydocument->rootelement ();
The output root element name, which is the output persons.
cout << rootelement->value () << Endl;
Gets the first person node.
Tixmlelement *firstperson = Rootelement->firstchildelement ();
Gets the name node and the age node and ID properties of the first person.
Tixmlelement *nameelement = Firstperson->firstchildelement ();
Tixmlelement *ageelement = Nameelement->nextsiblingelement ();
Tixmlattribute *idattribute = Firstperson->firstattribute ();
Outputs the name content of the first person, the week star, the age content, that is, the 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);
}
TinyXML: a good C + + XML parser