Windows Client development-use the tinyxml library to parse xml files

Source: Internet
Author: User

Windows Client development-use the tinyxml library to parse xml files
For example, in the duilib library used by windows clients, the interface is described in xml.

So today, let's talk about how to parse xml in the windows client, that is, C ++.

Most of the time, we use the. ini file to store some data.

Xml does have many advantages. To some extent, it can completely replace ini, but it is not as powerful as some people advocate.

Xml is very convenient for describing complex data structures, but it is a little more difficult to use than ini. When short configurations are expressed, no ini is concise. In addition, because of its strict format review mechanism, the fault tolerance is not very good, and errors may easily occur during handwriting. Aside from the configuration function, as a means of storing and transmitting data, xml also has a disadvantage: its processing and storage efficiency is very low, the parsing speed is slow, and more storage space is occupied.

Ini, which is usually used to configure and record software parameters. The advantage is that it is easy to use, and the embedded program is easy. Several interfaces are enough and easy to grasp. The configuration file is smaller, and manual configuration is easier. The disadvantage is that its structure is only two layers, which makes it powerless to describe complex types of data. In addition, the size of INI files is limited to 64 KB.

For small and simple configurations, ini is undoubtedly a more concise and convenient implementation method. Xml is suitable for more complex requirements.

How to read and write INI files has been introduced in our previous blog. windows provides us with APIs.

Now let's talk about how to parse xml.

Here, we use an Open Source library calledTinyxml

Source code:

Https://sourceforge.net/projects/tinyxml/click to open the link.

The source code is not long. We can directly introduce the source code.

First, write an xml file. We recommend that you useNotepad ++Instead of using the notepad that comes with windows.

Create an xml, test. xml

 
   
    
      
   
    
MinimizeTips
       
   
    
Minimize
     
      
      
   
    
MaximizeTips
       
   
    
Maximize
     
  
  
 

 


Next, we will read the above xml and store it in map as key-value. We will not go into details about the use of map, which has been introduced in previous blogs.

First, create an XMLDocument object;

Then, import the file through the xml Path.

You can use RootElement to obtain the root node.

You can use FirstChildElement to obtain the first subnode.

You can use NextSiblingElement to obtain the next node.

You can use the GetText method to obtain the strings in the node.

 

 

void ReadXmlToMap(const std::string& path, std::map
 
  & string_map){tinyxml2::XMLDocument doc;doc.LoadFile(path.c_str());tinyxml2::XMLElement *root = doc.RootElement();tinyxml2::XMLElement *node = root->FirstChildElement("String");while (node){tinyxml2::XMLElement *key_element = node->FirstChildElement();tinyxml2::XMLElement *value_element = key_element->NextSiblingElement();string_map.insert(std::pair
  
   (key_element->GetText(), value_element->GetText()));node = node->NextSiblingElement();}}
  
 

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.