zoukankan      html  css  js  c++  java
  • C++它tinyXML使用

     tinyXML一个非常好的操作C++图书馆,文件不大,但方法非常丰富。和apache的Dom4j能够披靡啊!

    习惯了使用java类库的我看到这么丰富的c++类库,非常高兴!它使用非常easy。仅仅须要拷贝几个文件到你的project中,没有STL也能够编译执行。

     
        
        以下我从这几个方面谈谈对tinyXML类库的使用以及理解。 

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

        然后解压缩tinyXML后,将这六个文件加入到你的c++project中,各自是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由文件陪同。

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    数据库锁表及阻塞的原因和解决办法
    JS中的this都有什么用?
    vue打包要点
    Vue.js的虚拟dom
    JS继承方式
    JS中的浅拷贝和深拷贝。
    详解计算机原码,反码,补码。
    经常被问到的面试题1
    eslint规则说明
    git常用命令总结
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4615970.html
Copyright © 2011-2022 走看看