zoukankan      html  css  js  c++  java
  • Tinyxml的简单应用

    参考文章:  1-> http://www.cnblogs.com/phinecos/archive/2008/03/11/1100912.html

                  2-> http://blog.csdn.net/clever101/article/details/5334369

    在日常开发过程中,常常需要用到xml文件的读写,tinyxml是一款轻量级的xml开源库,对于诸如程序配置,账单记录等常见的xml文件读写,tinyxml完全可以胜任.

    1->代码下载:http://sourceforge.net/projects/tinyxml/

    2->编译

      解压下载包,用VS2010打开 tinyxml.sln 解决方案,里面共有四个工程,编译第一个工程 tinyxml ,生成 tinyxml.lib 静态库文件,我们所需要用到的就是 tinyxml.h 和 tinyxml.lib .

    3->测试

      新建工程,添加tinyxml.h头文件和tinyxml.lib静态库,我们就可以对xml文件进行都写了,以下是测试代码.

     

    #include <iostream>
    #include <string>
    #include "tinystr.h"
    #include "tinyxml.h"
    using namespace std;
    
    #define ASSERT_P(v,s,r) do{if(v==s)return r;} while(0);
    
    bool CreateFile(const char* szFileName)
    {
        // 创建文档对象
        TiXmlDocument *pDocument = new TiXmlDocument();
        ASSERT_P(pDocument,NULL,false);
        // 创建根元素,连接到pDocument
        TiXmlElement *pConfigElement = new TiXmlElement("config");
        ASSERT_P(pConfigElement,NULL,false);
        pDocument->LinkEndChild(pConfigElement);
        // 创建一个common元素,连接到config下
        TiXmlElement *pCommonElement = new TiXmlElement("common");
        ASSERT_P(pCommonElement,NULL,false);
        // 为common元素设置属性(key,value)
        pCommonElement->SetAttribute("zoneid","1001");
        pCommonElement->SetAttribute("platid","1");
        pConfigElement->LinkEndChild(pCommonElement);
        // 添加zone元素
        int nZoneCnt = 10;
        TiXmlElement *pZoneListElement = new TiXmlElement("zonelist");
        ASSERT_P(pZoneListElement,NULL,false);
        pZoneListElement->SetAttribute("zonecount",10);
        pConfigElement->LinkEndChild(pZoneListElement);
        for(int i=0;i<10;i++)
        {
            char szName[50];
            TiXmlElement *pZoneElement = new TiXmlElement("zone");
            sprintf(szName,"zone_%02d",i+1);
            pZoneElement->SetAttribute("name",szName);
            pZoneListElement->LinkEndChild(pZoneElement);
        }
        return pDocument->SaveFile(szFileName);
    }
    
    bool ReadFile(const char *szFileName)
    {
        // 加载文件
        TiXmlDocument *pDocument = new TiXmlDocument(szFileName);
        ASSERT_P(pDocument,NULL,false);
        pDocument->LoadFile();
        // 获得,并输出根元素
        TiXmlElement *pConfigElement = pDocument->RootElement();
        cout<<pConfigElement->Value()<<endl;
        // 获取config的第一个子元素
        TiXmlElement *pCommonElement = pConfigElement->FirstChildElement();
        // or TiXmlElement *pCommonElement = pConfigElement->FirstChildElement("common");
        ASSERT_P(pCommonElement,NULL,false);
        cout<<"	zoneid="<<pCommonElement->Attribute("zoneid")<<",platid="<<pCommonElement->Attribute("platid")<<endl;
        // 第二个子元素
        TiXmlElement *pZoneListElement = pConfigElement->FirstChildElement("zonelist");
        ASSERT_P(pZoneListElement,NULL,false);
        int nZoneCount = atoi(pZoneListElement->Attribute("zonecount"));
        TiXmlElement *pZoneElement = pZoneListElement->FirstChildElement();
        for(int i=0;i<nZoneCount;i++)
        {
            ASSERT_P(pZoneElement,NULL,false);
            cout<<"		name="<<pZoneElement->Attribute("name")<<endl;
            pZoneElement = pZoneElement->NextSiblingElement();
        }
        return true;
    }
    
    int main()
    {
        const char *szFileName = "./config.xml";
        CreateFile(szFileName);
        ReadFile(szFileName);
        system("pause");
        return 0;
    }
  • 相关阅读:
    Oracle SQL语句大全(一)
    数据查询(3)-复杂查询(芮)
    数据查询(2)-高级查询(芮)
    数据查询(1)-简单查询(芮)
    T-SQL(5)-操作数据行(芮)
    T-SQL(4)-功能函数(芮)
    T-SQL(3)-批处理(芮)
    T-SQL(2)-逻辑控制(芮)
    T-SQL(1)-变量(芮)
    如何设计数据库(2)?(芮)
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/5095078.html
Copyright © 2011-2022 走看看