最近正在做的一个项目里涉及到xml文件的存取,同事推荐的tinyxml++ ,是c++版本的xml解析包,据说很小巧,好用,于是download了一个下来.顺便在其官方主页上下载了premake.exe用来生成工程文件的.以下是premake的用法.
 
Windows
premake --target cb-gcc [--unicode] [--ticpp-shared] [--dynamic-runtime]
Linux
premake --target cb-gcc [--unicode] [--ticpp-shared] [--dynamic-runtime]
 
我这里是默认生成CodeBlocks for gcc的工程文档.
生成之后打开文件进行编译,得到两个文件.libticpp.a, libticppd.a  在需要用到的工程里链接就行了.
 
具体的编译配置网上已经有很多教程,不明白的可以去看.
 
 
先看看运行效果图
 
下面开始针对tinyxml编程.
1:新建一个xml文档 取名 config.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<ApplictionConfig>
        <RoNum>0</RoNum>
        <AutoStart>0</AutoStart>
        <TimeInterval>100</TimeInterval>
        <BgPic>D:\devCode\desktopDev\new iLed\sound\main.wav</BgPic>
        <BgMusic>D:\devCode\desktopDev\new iLed\pic\4.png</BgMusic>
</ApplictionConfig>
 
一共5个属性配置.
然后开始写代码.
ilcdDemoReadConfig.h
InBlock.gif#ifndef ILCDSETXMLCONFIG_H_INCLUDED
InBlock.gif#define ILCDSETXMLCONFIG_H_INCLUDED
InBlock.gif
InBlock.gif                                //是否自启动字符串定义
InBlock.gif                                const char* AutoStartStr;
InBlock.gif                                //时间间隔字符串定义
InBlock.gif                                const char* TimeIntervalStr;
InBlock.gif                                //背景图片字符串定义
InBlock.gif                                const char* BgPicStr;
InBlock.gif                                //背景音乐字符串定义
InBlock.gif                                const char* BgMusicStr;
InBlock.gif                                //循环轨迹次数字符串定义
InBlock.gif                                const char* RotateNumStr;
InBlock.gif
InBlock.gif
InBlock.gif#endif // ILCDSETXMLCONFIG_H_INCLUDED
 
 
 
iLcdSetMain.cpp中读取文档的部分
记得要加入 一下两个头文件
InBlock.gif#include "tinyxml.h" // TinyXML的头文件
InBlock.gif#include "tinystr.h" // TinyXML的头文件
InBlock.gifvoid iLcdSetFrame::ReadXmlFile()
InBlock.gif{
InBlock.gif        TiXmlDocument doc("config.xml");
InBlock.gif        doc.LoadFile();
InBlock.gif        TiXmlElement* root = doc.FirstChildElement("ApplictionConfig");
InBlock.gif        if (root)//检测主节点ApplictionConfig是否存在
InBlock.gif        {
InBlock.gif                //自启动节点
InBlock.gif                TiXmlElement* AutoStartElement = root->FirstChildElement("AutoStart" );
InBlock.gif                //时间间隔节点
InBlock.gif                TiXmlElement* TimeIntervalElement = root->FirstChildElement("TimeInterval" );
InBlock.gif                //背景图片节点
InBlock.gif                TiXmlElement* BgPicElement = root->FirstChildElement("BgPic" );
InBlock.gif                //背景音乐节点
InBlock.gif                TiXmlElement* BgMusicElement = root->FirstChildElement("BgMusic" );
InBlock.gif                //循环次数节点
InBlock.gif                TiXmlElement* RoNumElement = root->FirstChildElement("RoNum");
InBlock.gif
InBlock.gif                //自启动节点检测开始
InBlock.gif                if (AutoStartElement)
InBlock.gif                {
InBlock.gif                        AutoStartStr = AutoStartElement->GetText();
InBlock.gif                        wxString ASS(AutoStartStr, wxConvUTF8);
InBlock.gif                        if (ASS.Cmp(wxT("1"))==0)
InBlock.gif                        {
InBlock.gif                                m_AutoStartCheck->SetValue(true);
InBlock.gif                        }
InBlock.gif                        else
InBlock.gif                        {
InBlock.gif                                m_AutoStartCheck->SetValue(false);
InBlock.gif                        }
InBlock.gif                }
InBlock.gif                //自启动节点检测结束
InBlock.gif                //时间间隔节点检测开始
InBlock.gif                if (TimeIntervalElement)
InBlock.gif                {
InBlock.gif                        TimeIntervalStr = TimeIntervalElement->GetText();
InBlock.gif                        wxString TIS(TimeIntervalStr, wxConvUTF8);
InBlock.gif                        m_txtTimeCheck->SetValue(TIS);
InBlock.gif                        //wxMessageBox(TIS, _("时间间隔"));
InBlock.gif                }
InBlock.gif                //时间间隔节点检测结束
InBlock.gif
InBlock.gif                //背景图片节点检测开始
InBlock.gif                if (BgPicElement)
InBlock.gif                {
InBlock.gif                        BgPicStr = BgPicElement->GetText();
InBlock.gif                        wxString BGS(BgPicStr, wxConvUTF8);
InBlock.gif                        m_txtBgPic->SetValue(BGS);
InBlock.gif                        //wxMessageBox(BGS, _("背景图片节点检测"));
InBlock.gif                }
InBlock.gif                //背景图片节点检测结束
InBlock.gif
InBlock.gif                //循环次数节点检测开始
InBlock.gif                if (RoNumElement)
InBlock.gif                {
InBlock.gif                        RotateNumStr=RoNumElement->GetText();
InBlock.gif                        wxString RN(RotateNumStr, wxConvUTF8);
InBlock.gif                        m_txtRoateNum->SetValue(RN);
InBlock.gif                }
InBlock.gif                //循环次数节点检测结束
InBlock.gif
InBlock.gif                //背景音乐节点检测开始
InBlock.gif                if (BgMusicElement)
InBlock.gif                {
InBlock.gif                        BgMusicStr = BgMusicElement->GetText();
InBlock.gif                        wxString BGRS(BgMusicStr, wxConvUTF8);
InBlock.gif                        m_txtBgMusic->SetValue(BGRS);
InBlock.gif                }
InBlock.gif
InBlock.gif                //背景图片节点检测结束
InBlock.gif
InBlock.gif        }
InBlock.gif
InBlock.gif}