We use the TinyXML2 Library to parse the operation XML
Download URL: http://grinninglizard.com/tinyxml2docs/index.html
This library is very simple, first we download the library from GitHub , as long as the tinyxml2.h and Tinyxml2.cpp copied to the project directory, add to the project.
Here is the common API read, a brief introduction
Direct code Bar, I feel that the writing has been perfect, do not want to see the classmate can copy directly , the connection of the space and line or storage according to the need to change on the line.
#include <iostream> #include "tinyxml2.h" using namespace tinyxml2;using namespace Std;void read_xml (XMLElement * Surface) {while (surface) {Const XmlAttribute *attr = Surface->firstattribute ();//Gets the first property value while (attr) {cout << "<< Attr->value (); attr = Attr->next (); Gets the next property value}const char* content = Surface->gettext (); Gets the content of the node if (content) {cout << "" << content << Endl;} XMLElement *surface1 = Surface->firstchildelement (); View whether the current object has child nodes if (Surface1) {read_xml (Surface1);//recursive call}surface = Surface->nextsiblingelement ();//The next node of the current object}} int main () {tinyxml2::xmldocument mydocument;//declares XML object mydocument. LoadFile ("Utf8test.xml"); Load XML file XmlElement *rootelement = mydocument. RootElement (); Gets the node XmlElement *surface = Rootelement->firstchildelement ();//Gets the first child node with value is empty by default, then returns the first node read_xml (surface); Cin.get ();}
XML Originals:
<?xml version= "1.0" encoding= "UTF-8"?><document> <english name= "name1" value= "value1" > The other side of the spec place <English> the world have many languages</english> <English> 222</english> < Xxx>xx <xx><x haha= "one" hehe= ">doubi</x></xx></xxx>" </English> <russian name= "name2" value= "value2" >Миримеетмногоязыков</Russian> <spanish name= "El nombre" Value= "El Valor" >el mundo tiene muchos idiomas</spanish> <simplifiedchinese name= "name" value= "Value" > There are many languages in the world </SimplifiedChinese> <русскийназвание= "name" ценность= "value" ><имеет></русский > < Chinese name = "Name" VALUE = "value" > The world has many languages </Chinese > <Heavy> "mëtæl!" </Heavy> <ä>umlaut element</ä></document>
Run Result: (garbled problem is the console current encoding is not UTF8 format encoding caused, do not worry)
C + + Read XML