Use of TINYXML2 library--MFC Engineering

Source: Internet
Author: User
Tags gettext

In the writing of the application, often need to dynamically load some data, in this case, Microsoft INI file is a good choice, but the platform is relatively poor versatility, the use of XML is relatively strong, but the resolution is more complex, model has developed a direct read and write XML library, C + + format, That's what we're talking about today, TINYXML2.

TINYXML2 has two main files, as follows

Use the time to put the door into his own application, as follows

Then import the library in the corresponding header file, as follows

" tinyxml2.h " using namespace tinyxml2;

Then I define an XML directory structure, as follows

<?XML version= "1.0" encoding = "Utf-8"?><Messages>    <button_messages>        <Button>            <name>Help</name>            <Promptmessage>Display Help information</Promptmessage>            <serialsendstring>Help</serialsendstring>        </Button>        <Button>            <name>List</name>            <Promptmessage>Show List of commands</Promptmessage>            <serialsendstring>List</serialsendstring>        </Button>

When using this library to load the file, load the file and then poll the element as follows

//Loading FilesXmlerror load =Configxmlfile.loadfile (fileName); if(Load! =xml_success) {MessageBox ("file load failed","Tips", MB_OK); Configfileloadok=false; return; }    //file loaded successfullyxmlelement*pelement; XMLElement*Hroot; //Load root nodeHroot =configxmlfile.rootelement (); if(!hroot) {MessageBox ("root node Error","Tips", MB_OK); return; } 

Load the root node first, then poll the child nodes in turn

//get All button data     Do     {        //Get Datanamestring = Pelement->firstchildelement ("name"),GetText (); Promptmessage= Pelement->firstchildelement ("Promptmessage"),GetText (); Serialsendstring= Pelement->firstchildelement ("serialsendstring"),GetText (); if(namestring) buttonname-Add (namestring); Buttonpromptmessage-Add (promptmessage); Buttonserialsendstring-Add (serialsendstring); Buttonindexcount++; Pelement= Pelement->nextsiblingelement ();//switch to the next child element} while(pelement);

This allows the XML file element to be loaded into the application, followed by the modification of the element, mainly the get method replaced by the set method, as follows

xmlelement*pelement; XMLElement*Hroot; //Load root nodeHroot =configxmlfile.rootelement (); if(!hroot) {MessageBox ("root node Error","Tips", MB_OK); return; }             //loading shortcut key informationPelement = Hroot->firstchildelement ("button_messages")->firstchildelement ("Button");//looking for a child elementButtonindexcount =0; //get All button data             Do{pelement->firstchildelement ("name")->settext (buttonname->GetAt (Buttonindexcount)); Pelement->firstchildelement ("Promptmessage")->settext (buttonpromptmessage->GetAt (Buttonindexcount)); Pelement->firstchildelement ("serialsendstring")->settext (buttonserialsendstring->GetAt (Buttonindexcount)); Buttonindexcount++; Pelement= Pelement->nextsiblingelement ();//switch to the next child element} while(pelement); inti = ((ccombobox*) (GetDlgItem (Idc_combo_config_file_item)))GetCurSel (); Char* FileName = (Char*)malloc(sizeof(Char)*255); FileName=Strpath.getat (i).            GetBuffer (); Configxmlfile.savefile (fileName);

Note The final savefile, ensure that your application has write permissions, some platforms need to turn on UAC control, the application interface is as follows

The usual engineering code.

http://download.csdn.net/detail/dengrengong/8610017

Use of TINYXML2 library--MFC Engineering

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.