Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate and parse XML files for C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how C++ generates and parses XML files". In daily operation, I believe many people have doubts about how C++ generates and parses XML files. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how C++ generates and parses XML files". Next, please follow the editor to study!

C++ generates and parses XML files

1.xml refers to Extensible markup language (EXtensible Markup Language)

2.xml is a markup language, similar to html

3.xml is designed to transmit data, not to display it

The 4.xml tag is not predefined. You need to define your own label.

The difference between XML and HTML

1.xml is not a substitute for html.

2.xml and html are designed for different purposes:

3.xml is designed to transfer and store data, focusing on the content of the data.

4.html is designed to display data, focusing on the appearance of the data.

5.html is designed to display information, while xml is designed to transmit information.

Third-party library

XML third-party parsing library, TinyXML,TinyXML is also an open source parsing XML parsing library, as simple as the name, for C++ development, support Windows and Linux. TinyXML traverses and analyzes XML through the DOM model.

Generate the XML file TiXmlDocument xmlDocument; / / add the XML declaration xmlDocument.LinkEndChild (new TiXmlDeclaration ("1. 0", "GBK", ""); / / add the root element TiXmlElement* xmlRoot = new TiXmlElement ("root"); xmlDocument.LinkEndChild (xmlRoot); / / add the child element 1 TiXmlElement* xmlChild1 = new TiXmlElement ("name") under the root element; xmlRoot- > LinkEndChild (xmlChild1) XmlChild1- > LinkEndChild (new TiXmlText ("woniu")); xmlChild1- > SetAttribute ("id", "0001"); / / set attributes / / add child elements under the root element 2 TiXmlElement* xmlChild2 = new TiXmlElement ("Student"); xmlRoot- > LinkEndChild (xmlChild2); TiXmlElement* xmlChild2_01 = new TiXmlElement ("name"); xmlChild2- > LinkEndChild (xmlChild2_01); xmlChild2_01- > LinkEndChild (new TiXmlText ("woniu201")) TiXmlElement* xmlChild2_02 = new TiXmlElement ("classes"); xmlChild2- > LinkEndChild (xmlChild2_02); xmlChild2_02- > LinkEndChild (new TiXmlText ("86")); / / Save xml file xmlDocument.SaveFile ("woniu.xml")

The generated XML is as follows:

Parse the XML file TiXmlDocument xmlDocument; if (! xmlDocument.LoadFile ("woniu.xml")) {return-1;} / root node TiXmlElement* xmlRoot = xmlDocument.RootElement (); if (xmlRoot = = NULL) {return-1;} / get child node information 1 TiXmlElement* xmlNode1Name = xmlRoot- > FirstChildElement ("name"); const char* node1Name = xmlNode1Name- > GetText () Const char* node1AttId = xmlNode1Name- > Attribute ("id"); / get child node information 2 TiXmlElement* xmlNode2Stu = xmlRoot- > FirstChildElement ("Student"); TiXmlElement* xmlNode2_name = xmlNode2Stu- > FirstChildElement ("name"); TiXmlElement* xmlname2_classes = xmlNode2Stu- > FirstChildElement ("classes"); const char* node2Name = xmlNode2_name- > GetText (); const char* node2Classes = xmlname2_classes- > GetText () At this point, the study of "how C++ generates and parses XML files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report