wxWidgets利用tinyxml实现xml解析

最近正在做的一个项目里涉及到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  在需要用到的工程里链接就行了.
 
具体的编译配置网上已经有很多教程,不明白的可以去看.
 
 
先看看运行效果图wxWidgets利用tinyxml实现xml解析_第1张图片
 
下面开始针对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
#ifndef ILCDSETXMLCONFIG_H_INCLUDED
#define ILCDSETXMLCONFIG_H_INCLUDED

                                //是否自启动字符串定义
                                const char* AutoStartStr;
                                //时间间隔字符串定义
                                const char* TimeIntervalStr;
                                //背景图片字符串定义
                                const char* BgPicStr;
                                //背景音乐字符串定义
                                const char* BgMusicStr;
                                //循环轨迹次数字符串定义
                                const char* RotateNumStr;


#endif // ILCDSETXMLCONFIG_H_INCLUDED
 
 
 
iLcdSetMain.cpp中读取文档的部分
记得要加入 一下两个头文件
#include "tinyxml.h" // TinyXML的头文件
#include "tinystr.h" // TinyXML的头文件
void iLcdSetFrame::ReadXmlFile()
{
        TiXmlDocument doc("config.xml");
        doc.LoadFile();
        TiXmlElement* root = doc.FirstChildElement("ApplictionConfig");
        if (root)//检测主节点ApplictionConfig是否存在
        {
                //自启动节点
                TiXmlElement* AutoStartElement = root->FirstChildElement("AutoStart" );
                //时间间隔节点
                TiXmlElement* TimeIntervalElement = root->FirstChildElement("TimeInterval" );
                //背景图片节点
                TiXmlElement* BgPicElement = root->FirstChildElement("BgPic" );
                //背景音乐节点
                TiXmlElement* BgMusicElement = root->FirstChildElement("BgMusic" );
                //循环次数节点
                TiXmlElement* RoNumElement = root->FirstChildElement("RoNum");

                //自启动节点检测开始
                if (AutoStartElement)
                {
                        AutoStartStr = AutoStartElement->GetText();
                        wxString ASS(AutoStartStr, wxConvUTF8);
                        if (ASS.Cmp(wxT("1"))==0)
                        {
                                m_AutoStartCheck->SetValue(true);
                        }
                        else
                        {
                                m_AutoStartCheck->SetValue(false);
                        }
                }
                //自启动节点检测结束
                //时间间隔节点检测开始
                if (TimeIntervalElement)
                {
                        TimeIntervalStr = TimeIntervalElement->GetText();
                        wxString TIS(TimeIntervalStr, wxConvUTF8);
                        m_txtTimeCheck->SetValue(TIS);
                        //wxMessageBox(TIS, _("时间间隔"));
                }
                //时间间隔节点检测结束

                //背景图片节点检测开始
                if (BgPicElement)
                {
                        BgPicStr = BgPicElement->GetText();
                        wxString BGS(BgPicStr, wxConvUTF8);
                        m_txtBgPic->SetValue(BGS);
                        //wxMessageBox(BGS, _("背景图片节点检测"));
                }
                //背景图片节点检测结束

                //循环次数节点检测开始
                if (RoNumElement)
                {
                        RotateNumStr=RoNumElement->GetText();
                        wxString RN(RotateNumStr, wxConvUTF8);
                        m_txtRoateNum->SetValue(RN);
                }
                //循环次数节点检测结束

                //背景音乐节点检测开始
                if (BgMusicElement)
                {
                        BgMusicStr = BgMusicElement->GetText();
                        wxString BGRS(BgMusicStr, wxConvUTF8);
                        m_txtBgMusic->SetValue(BGRS);
                }

                //背景图片节点检测结束

        }

}

你可能感兴趣的:(wxWidgets利用tinyxml实现xml解析)