Read and write XML project configuration file

Source: Internet
Author: User

  TinyXML is an open source parsing XML parsing library, can be used in C + +, can be compiled in Windows or Linux, using TinyXML for C + + XML parsing, simple and easy to use.
The model of this analytic library parses the XML file and then generates the DOM model in memory, which makes it easy to traverse the XML tree.

TinyXML version:tinyxml_2_6_2

Running environment Windows

There are two headers in it. tinystr.h tinyxml.h
Four source files tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp
A testing test file xmltest.cpp

Tixmlbase: The base class for the entire TinyXML model. Tixmlattribute: A property that corresponds to an element in XML. Tixmlnode: Corresponds to a node in the DOM structure. Tixmlcomment: Corresponds to the comment in the XML. Tixmldeclaration: Corresponds to the declaration part of XML, namely < versiong="1.0" ?>. Tixmldocument: The entire document that corresponds to XML. Tixmlelement: The element that corresponds to the XML. Tixmltext: Corresponds to the text portion of the XML. Tixmlunknown: Corresponds to an unknown part of XML. Tixmlhandler: Defines some operations for XML. 

To create a simple XML

<?xml version= "1.0" encoding= "UTF-8"?>  <school name= "Computer Academy" >      <class name = "C + +" >          < Student name= "Wang Ling" number= "052140" >              <email>[email protected]</email>              <address> Henan </ address>                   </Student>          <student name= "song June" number= "024561" >              <email>[email protected] </email>              <address> Hebei </address>                   </Student>      </Class>  </school >

Read operations

void__fastcall Tform1::readschoolxml () {using namespacestd; Const Char* XMLFile ="Studentconfig.xml";    Tixmldocument Doc; if(Doc. LoadFile (xmlfile)) {doc.    Print (); }    Else{cout<<"can not parse XML studentconfig.xml"<<Endl; return; } tixmlelement* rootelement = doc. RootElement ();//School Elementstixmlelement* classelement = Rootelement->firstchildelement ();//class Elementtixmlelement* studentelement = Classelement->firstchildelement ();//Students     for(; Studentelement! = NULL; studentelement = studentelement->nextsiblingelement ()) {Tixmlattribute* attributeofstudent = Studentelement->firstattribute ();//get the Name property of student         for(; Attributeofstudent! = NULL; attributeofstudent = attributeofstudent->Next ()) {cout<< Attributeofstudent->name () <<" : "<< Attributeofstudent->value () <<Std::endl; } tixmlelement* studentcontactelement = Studentelement->firstchildelement ();//get student's first contact information         for(; Studentcontactelement! = NULL; studentcontactelement = studentcontactelement->nextsiblingelement ()) {            stringContactType = studentcontactelement->Value (); stringContactvalue = studentcontactelement->GetText (); Std::cout<< ContactType <<" : "<< Contactvalue <<Std::endl; }    }}

Storage Operations

void__fastcall TForm1:: Writeschoolxml () {using namespacestd; Const Char* XMLFile ="Conf/school-write.xml";    Tixmldocument Doc; Tixmldeclaration* Decl =NewTixmldeclaration ("1.0","",""); Tixmlelement* Schoolelement =NewTixmlelement ("School" ); Tixmlelement* Classelement =NewTixmlelement ("Class" ); Classelement->setattribute ("name","C + +"); Tixmlelement* Stu1element =NewTixmlelement ("Student"); Stu1element->setattribute ("name","Wang Ling"); Stu1element->setattribute (" Number","052140"); Tixmlelement* Stu1emailelement =NewTixmlelement ("Email"); Stu1emailelement->linkendchild (NewTixmltext ("[email protected]") ); Tixmlelement* Stu1addresselement =NewTixmlelement ("Address"); Stu1addresselement->linkendchild (NewTixmltext ("Henan")); Stu1element-Linkendchild (stu1emailelement); Stu1element-Linkendchild (stu1addresselement); Tixmlelement* Stu2element =NewTixmlelement ("Student"); Stu2element->setattribute ("name","Song June"); Stu2element->setattribute (" Number","024561"); Tixmlelement* Stu2emailelement =NewTixmlelement ("Email"); Stu2emailelement->linkendchild (NewTixmltext ("[email protected]")); Tixmlelement* Stu2addresselement =NewTixmlelement ("Address"); Stu2addresselement->linkendchild (NewTixmltext ("Hebei")); Stu2element-Linkendchild (stu2emailelement); Stu2element-Linkendchild (stu2addresselement); Classelement-Linkendchild (stu1element); Classelement-Linkendchild (stu2element); Schoolelement-Linkendchild (classelement); Doc.    Linkendchild (decl); Doc.    Linkendchild (schoolelement); Doc. SaveFile (xmlfile);}

Read and write XML project configuration file

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.