Tinyxml commonly used C ++ XML parser is very good

Source: Internet
Author: User

Reading and setting xml configuration files is the most common operation. I tried several C ++ XML parsers. I personally think TinyXML is the most comfortable to use, because its API interface is very similar to Java, It is very object-oriented.
TinyXML is an open-source parsing XML library that can be used in C ++ and compiled in Windows or Linux. The parsing library's model parses the XML file and then generates the DOM model in the memory, so that we can easily traverse this XML tree.
The DOM model is a Document Object Model. It divides a document into multiple elements (such as books, chapters, sections, and segments ), the tree structure is used to represent the ordered relationship between these elements and the nested inclusion relationship.
The following is an XML snippet:

Copy codeThe Code is as follows: <Persons>
<Person ID = "1">
<Name> Zhou Xingxing </name>
<Age> 20 </age>
</Person>
<Person ID = "2">
<Name> Bai Jingjing </name>
<Age> 18 </age>
</Person>
</Persons>

In TinyXML, some classes are defined based on various elements of XML:
TiXmlBase: The base class of the entire TinyXML model.
TiXmlAttribute: attribute corresponding to the element in XML.
TiXmlNode: corresponds to a node in the DOM structure.
TiXmlComment: corresponding to the comment in XML
TiXmlDeclaration: corresponds to the declarative part in XML, that is, <? Versiong = "1.0"?>.
TiXmlDocument: corresponds to the entire XML document.
TiXmlElement: Elements corresponding to XML.
TiXmlText: text part corresponding to XML
TiXmlUnknown: corresponds to the unknown part of XML.
TiXmlHandler: defines some XML operations.
TinyXML is a parsing library mainly composed of DOM model classes (TiXmlBase, TiXmlNode, TiXmlAttribute, TiXmlComment, TiXmlDeclaration, TiXmlElement, TiXmlText, TiXmlUnknown) and Operation class (TiXmlHandler. It consists of two header files (. h file) and four CPP files (. cpp file), when used, as long as (tinyxml. h, tinystr. h, tinystr. cpp, tinyxml. cpp, tinyxmlerror. cpp, tinyxmlparser. cpp) import the project to use it. If needed, you can call it as your own DLL. For example, we can illustrate everything...
Corresponding XML file:Copy codeThe Code is as follows: <Persons>
<Person ID = "1">
<Name> phinecos </name>
<Age> 22 </age>
</Person>
</Persons>

Program code for reading and writing XML files:Copy codeThe Code is as follows: # include <iostream>
# Include "tinyxml. h"
# Include "tinystr. h"
# Include <string>
# Include <windows. h>
# Include <atlstr. h>
Using namespace std;
CString GetAppPath ()
{// Obtain 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. szFilePath is the path to save the file. If the file is created successfully, true is returned; otherwise, false is returned.
Try
{
// Create 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 it.
TiXmlElement * PersonElement = new TiXmlElement ("Person ");
RootElement-> LinkEndChild (PersonElement );
// Set the attributes of the Person element.
PersonElement-> SetAttribute ("ID", "1 ");
// Create and connect the name and age elements.
TiXmlElement * NameElement = new TiXmlElement ("name ");
TiXmlElement * AgeElement = new TiXmlElement ("age ");
PersonElement-> LinkEndChild (NameElement );
PersonElement-> LinkEndChild (AgeElement );
// Set and connect the content of the name and age elements.
TiXmlText * NameContent = new TiXmlText (" ");
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 it to the file
}
Catch (string & e)
{
Return false;
}
Return true;
}
Bool ReadXmlFile (string & szFileName)
{// Read the Xml file and traverse it
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 ();
// Obtain the root element, namely, Persons.
TiXmlElement * RootElement = myDocument-> RootElement ();
// Output the root element name, namely, the output Persons.
Cout <RootElement-> Value () <endl;
// Obtain the first Person node.
TiXmlElement * FirstPerson = RootElement-> FirstChildElement ();
// Obtain the name and age nodes and ID attributes of the first Person.
TiXmlElement * NameElement = FirstPerson-> FirstChildElement ();
TiXmlElement * AgeElement = NameElement-> NextSiblingElement ();
TiXmlAttribute * idattriperson = FirstPerson-> FirstAttribute ();
// Output the name of the first Person, that is, Zhou Xing; 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.