Rapidxml is a fast XML library that is 50-100 times faster than TinyXML. This article gives the source code for creating, reading, and writing XML. Since Sina Blog does not support text file upload, in the use of the following code need to download Rapidxml, about this library is: official website:https://sourceforge.net/projects/rapidxml/, the source of this library only four files are: about Raidxml information related to the introduction of the Internet has a lot of information, here I do not repeat the introduction, the following direct code:
#include"stdafx.h"#include"stdlib.h"#include<iostream>//The following three files are the library files required for this section of the Code#include"rapidxml/rapidxml.hpp"#include"rapidxml/rapidxml_utils.hpp"#include"rapidxml/rapidxml_print.hpp"intcreatexml ();intreadandchangexml ();int_tmain (intARGC, _tchar*argv[]) { //Test CasesCreatexml (); //Test CasesReadandchangexml (); System ("Pause"); return 0;}//Create a name called Config2.xml fileintCreatexml () {rapidxml::xml_document<>Doc; Rapidxml::xml_node<>* rot = Doc.allocate_node (rapidxml::node_pi,doc.allocate_string ("XML version= ' 1.0 ' encoding= ' utf-8 '")); Doc.append_node (ROT); Rapidxml::xml_node<>* node = Doc.allocate_node (Rapidxml::node_element,"Config","Information"); Doc.append_node (node); Rapidxml::xml_node<>* color = Doc.allocate_node (Rapidxml::node_element,"Color", NULL); Node-Append_node (color); Color->append_node (Doc.allocate_node (Rapidxml::node_element,"Red","0.1")); Color->append_node (Doc.allocate_node (Rapidxml::node_element,"Green","0.1")); Color->append_node (Doc.allocate_node (Rapidxml::node_element,"Blue","0.1")); Color->append_node (Doc.allocate_node (Rapidxml::node_element,"Alpha","1.0")); Rapidxml::xml_node<>* size = Doc.allocate_node (Rapidxml::node_element,"size", NULL); Size->append_node (Doc.allocate_node (Rapidxml::node_element,"x","640")); Size->append_node (Doc.allocate_node (Rapidxml::node_element,"y","480")); Node-Append_node (size); Rapidxml::xml_node<>* mode = Doc.allocate_node (Rapidxml::node_element,"Mode","Screen mode"); Mode->append_attribute (Doc.allocate_attribute ("fullscreen","false")); Node-append_node (mode); STD::stringtext; Rapidxml::p rint (Std::back_inserter (text), Doc,0); Std::cout<<text<<Std::endl; Std::ofstream out(".. /config/config1.xml"); out<<Doc; return 0;}//Read and modify Config3.xmlintReadandchangexml () {Rapidxml::file<> Fdoc (".. /config/config2.xml"); Std::cout<<fdoc.data () <<Std::endl; Rapidxml::xml_document<>Doc; Doc.parse<0>(Fdoc.data ()); Std::cout<<doc.name () <<Std::endl; //! Get root noderapidxml::xml_node<>* root =Doc.first_node (); Std::cout<<root->name () <<Std::endl; //get the first node of the root noderapidxml::xml_node<>* Node1 = root->First_node (); Std::cout<<node1->name () <<Std::endl; Rapidxml::xml_node<>* Node11 = node1->First_node (); Std::cout<<node11->name () <<Std::endl; Std::cout<<node11->value () <<Std::endl; //After you modify it, save it again.rapidxml::xml_node<>* size = Root->first_node ("size"); Size->append_node (Doc.allocate_node (Rapidxml::node_element,"W","1")); Size->append_node (Doc.allocate_node (Rapidxml::node_element,"h","1")); STD::stringtext; Rapidxml::p rint (Std::back_inserter (text), Doc,0); Std::cout<<text<<Std::endl; Std::ofstream out(".. /config/config2.xml"); out<<Doc; return 0;}
Rapidxml usage and examples in C + + (source code)