zoukankan      html  css  js  c++  java
  • [原][库][c++]tinyxml使用小结

    参考:http://blog.csdn.net/L_Andy/article/details/40615517

    tinyxml官网:

    http://www.grinninglizard.com/tinyxml/

    官方文档:

    http://www.grinninglizard.com/tinyxmldocs/index.html

    github上文件下载地址:
    https://sourceforge.net/projects/tinyxml/

    TinyXML类:


    TiXmlBase:整个TinyXML模型的基类。
    TiXmlAttribute:对应于XML中的元素的属性。
    TiXmlNode:对应于DOM结构中的节点。
    TiXmlComment:对应于XML中的注释。<!--XXXXX.-->
    TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
    TiXmlDocument:对应于XML的整个文档。
    TiXmlElement:对应于XML的元素。
    TiXmlText:对应于XML的文字部分。
    TiXmlUnknown:对应于XML的未知部分。
    TiXmlHandler:定义了针对XML的一些操作。

    使用:

    XML:

    <?xml version="1.0" encoding="UTF-8"?>  
    <phonebook>  
        <!--one item behalfs one contacted person.-->  
        <item>  
            <name>miaomaio</name>  
        <addr>Shaanxi Xi'an</addr>  
        <tel>13759911917</tel>  
        <email>miaomiao@home.com</email>  
        </item>  
        <item>  
            <name>gougou</name>  
        <addr>Liaoning Shenyang</addr>  
        <tel>15840330481</tel>  
        <email>gougou@home.com</email>  
        </item>  
        <!--more contacted persons.-->  
    </phonebook>  

    C++:

    //______________________________________________________________________  
    // Read information from xml file.  
      
    // define xml file path, as follow , we use relative path,  
    // but you can use absolute path also.  
    const char* filepath = "phonebookdata.xml";  
    TiXmlDocument doc(filepath);  
    bool loadOkay = doc.LoadFile();  
    // faile to load 'phonebookdata.xml'.  
    if (!loadOkay) {      
        printf( "Could not load test file %s. Error='%s'. Exiting.
    ", filepath,doc.ErrorDesc() );  
        exit( 1 );  
    }  
      
    // get dom root of 'phonebookdata.xml', here root should be 'phonebook'.  
    TiXmlElement* root = doc.RootElement();  
      
    printf("_______________________________________
    
    ");  
    printf("     contacted person information      
    
    ");  
    // trace every items below root.  
    for( TiXmlNode*  item = root->FirstChild( "item" );  
             item;  
             item = item->NextSibling( "item" ) ) {  
        printf("_______________________________________
    ");  
      
        // read name.  
        TiXmlNode* child = item->FirstChild();  
        const char* name = child->ToElement()->GetText();  
        if (name) {  
            printf("name:%s
    ",name);  
        } else {  
            printf("name:
    ");  
        }  
      
        // read address.  
        child = item->IterateChildren(child);  
        const char* addr = child->ToElement()->GetText();  
        if (addr) {  
            printf("addr:%s
    ",addr);  
        } else {  
            printf("addr:
    ");  
        }  
      
      
        // read telephone no.  
        child = item->IterateChildren(child);  
        const char* tel = child->ToElement()->GetText();  
            if (tel) {  
            printf("tel:%s
    ",tel);  
        } else {  
            printf("tel:
    ");  
        }  
      
        // read e-mail.  
        child = item->IterateChildren(child);  
        const char* email = child->ToElement()->GetText();  
        if(email) {  
            printf("email:%s
    ",email);  
        } else {  
            printf("email:
    ");  
        }  
          
        printf("
    ");  
      
    }  
    //______________________________________________________________________  
      
      
    //______________________________________________________________________  
    // Add information to xml file and save it.  
    TiXmlElement* writeRoot = doc.RootElement();  
    TiXmlNode* newNode = new TiXmlElement("item");  
      
       const TiXmlNode* name4NewNode = new TiXmlElement("name");  
    newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi"));  
      
    const TiXmlNode* addr4NewNode = new TiXmlElement("addr");  
    newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang"));  
      
    const TiXmlNode* tel4NewNode = new TiXmlElement("tel");  
    newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627"));  
      
    const TiXmlNode* email4NewNode = new TiXmlElement("email");  
    newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com"));  
      
    writeRoot->InsertEndChild(*newNode);  
    doc.SaveFile();  
    //______________________________________________________________________  
  • 相关阅读:
    Pyhon基础过滤字符串中的字母数字特殊符号
    [编程题] 斐波那契数列
    左旋转字符串(Java)-循环Index方式
    [编程题]字符流中第一个不重复的字符
    6525. 【2020.4.1模拟】Valleys
    6515. 【GDOI2020模拟03.28】数一数(one)
    6516. 【GDOI2020模拟03.28】数二数(two)
    6508. 【GDOI2020模拟03.11】我的朋友们
    6494. 【GDOI2020模拟03.08】勘探
    6491. 【GDOI2020模拟03.04】铺路
  • 原文地址:https://www.cnblogs.com/lyggqm/p/7199055.html
Copyright © 2011-2022 走看看