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();  
    //______________________________________________________________________  
  • 相关阅读:
    LeetCode 32. 最长有效括号(Longest Valid Parentheses)
    LeetCode 141. 环形链表(Linked List Cycle)
    LeetCode 160. 相交链表(Intersection of Two Linked Lists)
    LeetCode 112. 路径总和(Path Sum)
    LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)
    LightGBM新特性总结
    sql service 事务与锁
    C#泛型实例详解
    C# 中的委托和事件(详解)
    C# DateTime日期格式化
  • 原文地址:https://www.cnblogs.com/lyggqm/p/7199055.html
Copyright © 2011-2022 走看看