Windows Client Development--parsing XML files using the TinyXML library

Source: Internet
Author: User


For example, in the Duilib library used by Windows clients, the interface is described in XML.


So, today we're going to talk about how XML is parsed in the Windows client, that is, C + +.


Many times, we use. ini files to store some data.


XML does have a lot of advantages, and to a certain extent it can completely replace INI, but it is not as if some people preach more than INI.

XML, which is very convenient for describing complex data structures, is a bit cumbersome relative to INI use. There is no INI conciseness when expressing a shorter configuration. And because it has a more rigorous format review mechanism, fault tolerance is not particularly good, in handwriting prone to errors. Aside from the configuration of the function, as a means of storing data transmission, XML has a disadvantage is that its processing and storage efficiency is very low, the resolution is slow, occupy more storage space.

INI, which is typically used to configure and record the parameters of the software. Advantage is easy to use, embedded program is also easy, a few interface is enough, it is easy to master. Configuration files are smaller and manual configuration is easier. The disadvantage is that it has a structure of only 2 layers, which makes it less difficult to depict complex types of data. Additionally the INI file has a size limit of 64KB.

For a small simple configuration, INI is undoubtedly a more concise and convenient way to implement. XML, however, is suitable for more complex requirements.

How to read and write INI file, our previous blog has been introduced, the Windows system provides us with the API.


Now let's talk about parsing XML.

Here, we use an open source library called TinyXML

Source:

https://sourceforge.net/projects/tinyxml/Click to open the link.

Source code is not long, we can directly introduce the source code.

First we write an XML. It is important to note that notepad++is recommended instead of using the Notepad that comes with Windows.

Create a Xml,test.xml

<?xml version= "1.0" encoding= "Utf-8"?><strings>  <!--system button-->  <String>    <StringKey>MinimizeTips</StringKey>    <StringValue> minimize </StringValue>  </ string>    <String>    <StringKey>MaximizeTips</StringKey>    <StringValue> Maximizing </StringValue>  </String><Strings>


Next, we are going to read the XML above and deposit it in the map as Key-value. We will not repeat the use of map, the previous blog has been introduced.

The first is to create a XmlDocument object;

And then, through the path to the XML, import the file

You can use RootElement to get the root node

You can use firstchildelement to get the first child node

can use Nextsiblingelement Get the next node

You can use the GetText method to get a string in a node


void Readxmltomap (const std::string& path, std::map<std::string, std::string>& 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::p air<std::string, Std::string> (Key_element->gettext (), value_element-> GetText ())); node = Node->nextsiblingelement ();}}



Windows Client Development--parsing XML files using the TinyXML library

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.