//------------------------------------------------------------------------------------------------------------- ----------------------------------
Download the//c++//xml/tinyxml.rar file from my web drive. Added to the catalog in the project. Then add the corresponding header file and the source file in Solution Explorer. This step must be done oh.
In this way, TinyXML is formally added to your project.
Then refer to it where needed:
#include " tinyxml.h "
#include " Tinystr.h "
All two must be referenced.
Then use the following software to operate.
In actual use, only six source files were used, their names are as follows:
I am using the VS2013 with blend version.
//------------------------------------------------------------------------------------------------------------- ------------------------------------
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></age> </Person> <person id="2"> <name> Bai jingjing </name> <age></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></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 namespacestd; CString Getapppath () {//get the application root directoryTCHAR Modulepath[max_path]; GetModuleFileName (NULL, Modulepath, MAX_PATH); CString Strmodulepath (Modulepath); Strmodulepath= Strmodulepath.left (Strmodulepath.reversefind (_t ('\\'))); returnStrmodulepath;}BOOLCreatexmlfile (string&szFileName) {//creates an XML file, Szfilepath the path to the file, or False if the creation succeeds returns true Try { //creates an XML Document object. Tixmldocument *mydocument =Newtixmldocument ();
Add an XML header.
Tixmldeclaration *pdeclaration = new Tixmldeclaration ("1.0", "UTF-8", "" ");
Mydocument->linkendchild (pdeclaration);
//create a root element and connect it. Tixmlelement *rootelement =NewTixmlelement ("Persons"); MyDocument-Linkendchild (rootelement); //Create a person element and connect. Tixmlelement *personelement =NewTixmlelement (" 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 =NewTixmlelement ("name"); Tixmlelement*ageelement =NewTixmlelement (" Age"); Personelement-Linkendchild (nameelement); Personelement-Linkendchild (ageelement);
//This is to add an attribute
Nameelement->setattribute ("Data", "192.168.1.12");
//sets the contents of the name element and the age element and connects. Tixmltext *namecontent =NewTixmltext ("Zhou Xing"); Tixmltext*agecontent =NewTixmltext (" A"); Nameelement-Linkendchild (namecontent); Ageelement-Linkendchild (agecontent); CString AppPath=Getapppath (); stringSeperator ="\\"; stringFullPath = Apppath.getbuffer (0) +seperator+szFileName; MyDocument->savefile (Fullpath.c_str ());//Save to file } Catch(string&e) {return false; } return true;}BOOLReadxmlfile (string&szFileName) {//read the XML file and traverse Try{CString AppPath=Getapppath (); stringSeperator ="\\"; stringFullPath = Apppath.getbuffer (0) +seperator+szFileName; //creates an XML Document object. Tixmldocument *mydocument =Newtixmldocument (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;}intMain () {stringFileName ="Info.xml"; Createxmlfile (FileName); Readxmlfile (fileName);}
TinyXML: a good C + + XML parser