c++ parses XML files with TINYXML

  • 2020-09-16 07:44:45
  • OfStack

TinyXML introduction

Recently, I did a small project of load balancing, which needed to parse xml configuration file. When I used TinyXML, I felt it was very easy to use. I gave a simple example of using TinyXML to parse XML, and many complex applications could be completed based on this example.

TinyXML is an open source parsing library for XML, which can be used for C++ and can be compiled in Windows or Linux. The model of the parsing library parses the XML file and generates the DOM model in memory, making it easy to walk through the XML tree.

The DOM model is the document object model, which divides the entire document into multiple elements (such as books, chapters, sections, segments, etc.) and USES a tree structure to represent the sequential relationships and nested containment relationships among these elements.

TinyXML class description

In TinyXML, 1 class is defined based on the various elements of XML:

TiXmlBase: The base class for the entire TinyXML model.

TiXmlAttribute: Attributes that correspond to elements in XML.

TiXmlNode: Corresponds to nodes in the DOM structure.

TiXmlComment: Corresponds to the comments in XML.

TiXmlDeclaration: Corresponds to the statement section in XML, i.e < ? versiong = "1.0" & # 63; > .

TiXmlDocument: Corresponds to the entire document of XML.

TiXmlElement: The element corresponding to XML.

TiXmlText: The literal portion of XML.

TiXmlUnknown: Corresponds to the unknown part of XML.

TiXmlHandler: Defines some operations for XML 1.

Download and compile

Download address: http: / / sourceforge net/projects/tinyxml /

The working directory is:


tinyxml/      // Working directory 
|-- include    // Header file root directory 
|  |-- tinyxml  //tinyxml Header file, including tinystr.h tinyxml.h
|-- src      //cpp Source file root directory 
  |-- tinyxml    //tinyxml Source folder, including tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp
  |-- main.cpp  // Our main function, call tinyxml Sample code for  
|-- conf      // In our example xml The folder in which the file resides 
|-- makefile    //makefile , need not we say more, do not understand look at my blog makefile Best practices 

Simple example

Create the ES89en.xml code in the conf directory


<School name=" Software college ">
  
  <Class name = "C++"> 
    
    <Student name="tinyxml" number="123"> 
      <email>tinyxml@163.com</email> 
      <address> China </address>      
    </Student> 
    
    <Student name="jsoncpp" number="456"> 
      <email>jsoncpp@gmail.com</email> 
      <address> The United States </address>      
    </Student> 
    
  </Class> 
  
</School>

To use tinyxml, simply include it in the header file < tinyxml.h > Can be

Read the entire xml file and print the code:


void printSchoolXml() {
  using namespace std;
  TiXmlDocument doc; 
  const char * xmlFile = "conf/school.xml";  
  if (doc.LoadFile(xmlFile)) {  
    doc.Print(); 
  } else {
    cout << "can not parse xml conf/school.xml" << endl;
  }  
}

Read XML


void readSchoolXml() {
  using namespace std;
  const char * xmlFile = "conf/school.xml";
  TiXmlDocument doc;               
  if (doc.LoadFile(xmlFile)) {
    doc.Print();
  } else {
    cout << "can not parse xml conf/school.xml" << endl;
    return;
  }
  
  TiXmlElement* rootElement = doc.RootElement(); //School The element  
  TiXmlElement* classElement = rootElement->FirstChildElement(); // Class The element 
  TiXmlElement* studentElement = classElement->FirstChildElement(); //Students 
  
  for (; studentElement != NULL; studentElement = studentElement->NextSiblingElement() ) {
    TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute(); // To obtain student the name attribute  
    for (;attributeOfStudent != NULL; attributeOfStudent = attributeOfStudent->Next() ) {
      cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl;    
    }                 

    TiXmlElement* studentContactElement = studentElement->FirstChildElement();// To obtain student The first 1 Contact information  
    
    for (; studentContactElement != NULL; studentContactElement = studentContactElement->NextSiblingElement() ) {
      string contactType = studentContactElement->Value();
      string contactValue = studentContactElement->GetText();
      cout << contactType << " : " << contactValue << std::endl;      
    }  
  } 
}

Write xml


void writeSchoolXml() {
  using namespace std;
  const char * xmlFile = "conf/school-write.xml"; 
  
  TiXmlDocument doc; 
  TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", ""); 
  TiXmlElement * schoolElement = new TiXmlElement( "School" ); 
  TiXmlElement * classElement = new TiXmlElement( "Class" ); 
  classElement->SetAttribute("name", "C++");

  TiXmlElement * stu1Element = new TiXmlElement("Student");
  stu1Element->SetAttribute("name", "tinyxml");
  stu1Element->SetAttribute("number", "123");
  TiXmlElement * stu1EmailElement = new TiXmlElement("email");
  stu1EmailElement->LinkEndChild(new TiXmlText("tinyxml@163.com") );
  TiXmlElement * stu1AddressElement = new TiXmlElement("address");
  stu1AddressElement->LinkEndChild(new TiXmlText(" China "));
  stu1Element->LinkEndChild(stu1EmailElement);
  stu1Element->LinkEndChild(stu1AddressElement);

  TiXmlElement * stu2Element = new TiXmlElement("Student");
  stu2Element->SetAttribute("name", "jsoncpp");
  stu2Element->SetAttribute("number", "456");
  TiXmlElement * stu2EmailElement = new TiXmlElement("email");
  stu2EmailElement->LinkEndChild(new TiXmlText("jsoncpp@163.com"));
  TiXmlElement * stu2AddressElement = new TiXmlElement("address");
  stu2AddressElement->LinkEndChild(new TiXmlText(" The United States "));
  stu2Element->LinkEndChild(stu2EmailElement);
  stu2Element->LinkEndChild(stu2AddressElement);

  classElement->LinkEndChild(stu1Element); 
  classElement->LinkEndChild(stu2Element); 
  schoolElement->LinkEndChild(classElement); 
  
  doc.LinkEndChild(decl); 
  doc.LinkEndChild(schoolElement); 
  doc.SaveFile(xmlFile); 
}

XML delete operation

Delete a node, TiXmlNode It's TiXmlElement, TiXmlComment, TiXmlText, TiXmlDeclaration, TiXmlUnknown,

TiXmlDocument base class


TiXmlNode node;
node.Clear();

Remove child node B from A node


TiXmlNode nodeA;
nodeA. RemoveChild( TiXmlNode* removeThis );

Removes the attribute named B from the element A


TiXmlAttribute attrA;
attrA. RemoveAttribute( const char * name );

XML modify operation

The search content is <mfid val="1234" /> Now you need to change 1234 to another value


TiXmlNode* lpnode = NULL;
lpnode = tixml.RootElement()->IterateChildren("mfid",lpnode);
TiXmlAttribute* tiattr = lpnode->ToElement()->FirstAttribute();
// find mfid Node, gets the value 1 Attribute values. Note that if there are multiple property values, you need to determine which one is required 
tiattr->SetValue(mfid.c_str());

Replace 1 node


TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );

Above is c++ TINYXML parsing XML file details, more about c++ tinyxml parsing XML information please pay attention to other related articles on this site!


Related articles: