C + + Read XML

Source: Internet
Author: User

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

Related Article

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.