zoukankan      html  css  js  c++  java
  • [转]C++之tinyXML使用

    原文地址:http://qaohao.iteye.com/blog/496237

    博客分类:
        tinyXML一款很优秀的操作C++类库,文件不大,但方法很丰富,和apache的Dom4j可以披靡啊!习惯了使用java类库的我看到这么丰富的c++类库,很高兴!它使用很简单,只需要拷贝几个文件到你的工程中,没有STL也可以编译运行。 
        
        下面我从这几个方面谈谈对tinyXML类库的使用以及理解。 

        首先在sourceforge上下载tinyXML类库,地址:http://sourceforge.net/projects/tinyxml/ 

        然后解压缩tinyXML后,将这六个文件添加到你的c++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在需要操作xml文件的地方,使用如下代码,就可以引入tinyXML类库。
    C++代码  收藏代码
    1. #include<tinyxml>  

    或者
    C++代码  收藏代码
    1. #include "tinyxml.h"  


        下面我用个简单的例子说明如何使用tinyXML操作xml文件。在讲例子之前我先说说tinyXML中主要类和xml文档之间的对应关系。下面是tinyXML中主要class的类图,反应各个类之间的静态关系。 

    引用来自tinyXML文档

        TiXmlBase是所有类的基类,TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,其中TiXmlNode类指的是所有被<...>...<.../>包括的内容,而xml中的节点又具体分为以下几方面内容,分别是声明、注释、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown,分别用来指明具体是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号里面的内容,像<... ***=...>,其中***就是一个属性。这块我具体用一个xml文档说明一下,内容如下: 
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <phonebook>  
    3.     <!--one item behalfs one contacted person.-->  
    4.     <item>  
    5.         <name>miaomaio</name>  
    6.     <addr>Shaanxi Xi'an</addr>  
    7.     <tel>13759911917</tel>  
    8.     <email>miaomiao@home.com</email>  
    9.     </item>  
    10.     <item>  
    11.         <name>gougou</name>  
    12.     <addr>Liaoning Shenyang</addr>  
    13.     <tel>15840330481</tel>  
    14.     <email>gougou@home.com</email>  
    15.     </item>  
    16.     <!--more contacted persons.-->  
    17. </phonebook>  

    • 像TiXmlDeclaration指的就是<?xml version="1.0" encoding="UTF-8"?>,
    • 像TiXmlComment指的就是<!--one item behalfs one contacted person.-->、 <!--more contacted persons.-->,
    • 像TiXmlDocument指的就是整个xml文档,
    • 像TiXmlElement指的就是<phonebook>、<item>、<name>、<addr>等等这些节点,
    • 像TiXmlText指的就是‘gougou’、‘15840330481’这些夹在<item>与</item>、<name>与</name>、<addr>与</addr>之间的文本文字,
    • 像TiXmlAttribute指的就是<?xml version="1.0" encoding="UTF-8"?>节点中version、encoding,
    • 除此之外就是TiXmlUnknown。


    下面是我自己写的一段读xml文件的c++代码,以及再往xml写入一个item的源代码,其中phonebookdata.xml中的内容就是上面xml,仅供参考。 
    C++代码  收藏代码
    1. //______________________________________________________________________  
    2. // Read information from xml file.  
    3.   
    4. // define xml file path, as follow , we use relative path,  
    5. // but you can use absolute path also.  
    6. const char* filepath = "phonebookdata.xml";  
    7. TiXmlDocument doc(filepath);  
    8. bool loadOkay = doc.LoadFile();  
    9. // faile to load 'phonebookdata.xml'.  
    10. if (!loadOkay) {      
    11.     printf( "Could not load test file %s. Error='%s'. Exiting. ", filepath,doc.ErrorDesc() );  
    12.     exit( 1 );  
    13. }  
    14.   
    15. // get dom root of 'phonebookdata.xml', here root should be 'phonebook'.  
    16. TiXmlElement* root = doc.RootElement();  
    17.   
    18. printf("_______________________________________ ");  
    19. printf("     contacted person information       ");  
    20. // trace every items below root.  
    21. for( TiXmlNode*  item = root->FirstChild( "item" );  
    22.          item;  
    23.          item = item->NextSibling( "item" ) ) {  
    24.     printf("_______________________________________ ");  
    25.   
    26.     // read name.  
    27.     TiXmlNode* child = item->FirstChild();  
    28.     const char* name = child->ToElement()->GetText();  
    29.     if (name) {  
    30.         printf("name:%s ",name);  
    31.     } else {  
    32.         printf("name: ");  
    33.     }  
    34.   
    35.     // read address.  
    36.     child = item->IterateChildren(child);  
    37.     const char* addr = child->ToElement()->GetText();  
    38.     if (addr) {  
    39.         printf("addr:%s ",addr);  
    40.     } else {  
    41.         printf("addr: ");  
    42.     }  
    43.   
    44.   
    45.     // read telephone no.  
    46.     child = item->IterateChildren(child);  
    47.     const char* tel = child->ToElement()->GetText();  
    48.         if (tel) {  
    49.         printf("tel:%s ",tel);  
    50.     } else {  
    51.         printf("tel: ");  
    52.     }  
    53.   
    54.     // read e-mail.  
    55.     child = item->IterateChildren(child);  
    56.     const char* email = child->ToElement()->GetText();  
    57.     if(email) {  
    58.         printf("email:%s ",email);  
    59.     } else {  
    60.         printf("email: ");  
    61.     }  
    62.       
    63.     printf(" ");  
    64.   
    65. }  
    66. //______________________________________________________________________  
    67.   
    68.   
    69. //______________________________________________________________________  
    70. // Add information to xml file and save it.  
    71. TiXmlElement* writeRoot = doc.RootElement();  
    72. TiXmlNode* newNode = new TiXmlElement("item");  
    73.   
    74.    const TiXmlNode* name4NewNode = new TiXmlElement("name");  
    75. newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi"));  
    76.   
    77. const TiXmlNode* addr4NewNode = new TiXmlElement("addr");  
    78. newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang"));  
    79.   
    80. const TiXmlNode* tel4NewNode = new TiXmlElement("tel");  
    81. newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627"));  
    82.   
    83. const TiXmlNode* email4NewNode = new TiXmlElement("email");  
    84. newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com"));  
    85.   
    86. writeRoot->InsertEndChild(*newNode);  
    87. doc.SaveFile();  
    88. //______________________________________________________________________  


    具体使用可以参考tinyxml随带的文档。
  • 相关阅读:
    Redis Cluter
    数据库设计范式
    kvm虚拟化
    架构前端
    集群架构
    初识shell编程
    网络知识
    Linux三剑客
    Linux磁盘管理
    高性能异步爬虫
  • 原文地址:https://www.cnblogs.com/Crysaty/p/5970262.html
Copyright © 2011-2022 走看看