About TinyXML

TinyXml快速入门(一)作者:朱金灿
来源:http://blog.csdn.net/clever101


      对于xml文件,目前我的工作只是集中在配置文件和作为简单的信息文件来用,因此我不太喜欢使用msxml这种重量级的xml解析器,特别是使用msxml解析xml涉及到复杂的com类型转换,更是令人感觉繁琐。因此对于简单的xml文件的解析,我更愿意使用开源的TinyXml。

 


      首先介绍一下TinyXml吧。TinyXML是目前非常流行的一款基于DOM模型的XML解析器,简单易用且小巧玲珑,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作,其主页是:http://www.grinninglizard.com/tinyxml/ ,目前最新版本是2.5.3 版本。

TinyXml网上的教程很多,但是我觉得写得都不怎样(感觉就是看完之后就没学会)。没办法,只得自己整理一篇适合自己的,至于适不适合别人,就见仁见智了。我感觉xml文件本质就是小型的数据库,换个角度来说就是,你对数据库有什么操作你对xml文件就应能实现什么操作。一般而言,对数据库的操作包括以下几种:新建数据库、查询数据库、修改数据库和删除数据库。那么对应xml文件就是新建xml文件、查询xml文件的指定节点的值,修改xml文件中节点的值和删除xml文件中节点的值。

 


       首先我们认识一下xml文件有哪几种形式。下面我列出一些常用的xml文件的形式:

 


view plaincopy to clipboardprint?
example1.xml:  
<?xml version="1.0" ?> 
<Hello>World</Hello> 
example2.xml:  
<?xml version="1.0" ?> 
<poetry> 
       <verse> 
               Alas  
                 Great World  
                       Alas (again)  
       </verse> 
</poetry> 
example3.xml:  
<?xml version="1.0" ?> 
<shapes> 
       <circle name="int-based" x="20" y="30" r="50" /> 
       <point name="float-based" x="3.5" y="52.1" /> 
</shapes> 
example4.xml:  
<?xml version="1.0" ?> 
<MyApp> 
    <Messages> 
        <Welcome>Welcome to MyApp</Welcome> 
        <Farewell>Thank you for using MyApp</Farewell> 
    </Messages> 
    <Windows> 
        <Window name="MainFrame" x="5" y="15" w="400" h="250" /> 
    </Windows> 
    <Connection ip="192.168.0.1" timeout="123.456000" /> 
</MyApp> 
example1.xml:
<?xml version="1.0" ?>
<Hello>World</Hello>
example2.xml:
<?xml version="1.0" ?>
<poetry>
       <verse>
               Alas
                 Great World
                       Alas (again)
       </verse>
</poetry>
example3.xml:
<?xml version="1.0" ?>
<shapes>
       <circle name="int-based" x="20" y="30" r="50" />
       <point name="float-based" x="3.5" y="52.1" />
</shapes>
example4.xml:
<?xml version="1.0" ?>
<MyApp>
    <Messages>
        <Welcome>Welcome to MyApp</Welcome>
        <Farewell>Thank you for using MyApp</Farewell>
    </Messages>
    <Windows>
        <Window name="MainFrame" x="5" y="15" w="400" h="250" />
    </Windows>
    <Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp>

 


      上面的例子摘自《TinyXML Tutorial 中文指南》。上面有四个例子,你看到了xml文件的几种表现形式?我看到了本质来说不过是两种表现形式:属性值值在尖括号内,如<Window name="MainFrame" x="5" y="15" w="400" h="250" />和文本在尖括号外,如<Welcome>Welcome to MyApp</Welcome>,具体如下图: 
 


     鉴于example4.xml比较复杂,下面我将以此为例介绍tinyxml的使用。

 


     Tinyxml使用了两种编译选择:使用标准C的char *类型或者使用STL中的std::string,其中使用预处理器TIXML_USE_STL进行控制,即添加了TIXML_USE_STL为使用std::string的。鉴于STL的广泛使用以及其强大功能,下面我以使用std::string的tinyxml说明。

首先使用VS 2005打开tinyxmlSTL.dsp的工程文件,将其编译成一个静态库,debug版本为:tinyxmld_STL.lib,然后开始测试tinyxml库。我的测试计划是这样的:首先使用tinyxml库创建example4.xml,然后将其读出来,然后查询指定节点的属性或文本,再修改example4.xml(修改其中的一些节点值和删除其中一个节点,增加一个节点),然后再读出来以判断是否修改成功。具体是在VS 2005上新建一个控制台工程:Test,注意使用多字节字符集进行编译,同时添加。首先是创建xml文件的代码:

 


     view plaincopy to clipboardprint?
 
bool CreateXml(std::string XmlFile)  
 
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument;  
    if (NULL==pDoc)  
    
        return false;  
    
    TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(_T("1.0"),_T(""),_T(""));  
    if (NULL==pDeclaration)  
    
        return false;  
    
    pDoc->LinkEndChild(pDeclaration);  
    // 生成一个根节点:MyApp  
    TiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));  
    if (NULL==pRootEle)  
    
        return false;  
    
    pDoc->LinkEndChild(pRootEle);  
    // 生成子节点:Messages  
    TiXmlElement *pMsg = new TiXmlElement(_T("Messages"));  
    if (NULL==pMsg)  
    
        return false;  
    
    pRootEle->LinkEndChild(pMsg);  
    // 生成子节点:Welcome  
    TiXmlElement *pWelcome = new TiXmlElement(_T("Welcome"));  
    if (NULL==pWelcome)  
    
        return false;  
    
    pMsg->LinkEndChild(pWelcome);  
    // 设置Welcome节点的值  
    std::string strValue = _T("Welcome to MyApp");  
    TiXmlText *pWelcomeValue = new TiXmlText(strValue);  
    pWelcome->LinkEndChild(pWelcomeValue);  
    // 生成子节点:Farewell  
    TiXmlElement *pFarewell = new TiXmlElement(_T("Farewell"));  
    if (NULL==pFarewell)  
    
        return false;  
    
    pMsg->LinkEndChild(pFarewell);  
    // 设置Farewell节点的值  
    strValue = _T("Thank you for using MyApp");  
    TiXmlText *pFarewellValue = new TiXmlText(strValue);  
    pFarewell->LinkEndChild(pFarewellValue);  
    // 生成子节点:Windows  
    TiXmlElement *pWindows = new TiXmlElement(_T("Windows"));  
    if (NULL==pWindows)  
    
        return false;  
    
    pRootEle->LinkEndChild(pWindows);  
    // 生成子节点:Window  
    TiXmlElement *pWindow = new TiXmlElement(_T("Window"));  
    if (NULL==pWindow)  
    
        return false;  
    
    pWindows->LinkEndChild(pWindow);  
    // 设置节点Window的值  
    pWindow->SetAttribute(_T("name"),_T("MainFrame"));  
    pWindow->SetAttribute(_T("x"),_T("5"));  
    pWindow->SetAttribute(_T("y"),_T("15"));  
    pWindow->SetAttribute(_T("w"),_T("400"));  
    pWindow->SetAttribute(_T("h"),_T("250"));  
    // 生成子节点:Window  
    TiXmlElement *pConnection  = new TiXmlElement(_T("Connection"));  
    if (NULL==pConnection)  
    
        return false;  
    
    pRootEle->LinkEndChild(pConnection);  
    // 设置节点Connection的值  
    pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));  
    pConnection->SetAttribute(_T("timeout"),_T("123.456000"));  
    pDoc->SaveFile(XmlFile);  
    return true;  
 

bool CreateXml(std::string XmlFile)
{
 // 定义一个TiXmlDocument类指针
 TiXmlDocument *pDoc = new TiXmlDocument;
 if (NULL==pDoc)
 {
  return false;
 }
 TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(_T("1.0"),_T(""),_T(""));
 if (NULL==pDeclaration)
 {
  return false;
 }
 pDoc->LinkEndChild(pDeclaration);
 // 生成一个根节点:MyApp
 TiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));
 if (NULL==pRootEle)
 {
  return false;
 }
 pDoc->LinkEndChild(pRootEle);
 // 生成子节点:Messages
 TiXmlElement *pMsg = new TiXmlElement(_T("Messages"));
 if (NULL==pMsg)
 {
  return false;
 }
 pRootEle->LinkEndChild(pMsg);
 // 生成子节点:Welcome
 TiXmlElement *pWelcome = new TiXmlElement(_T("Welcome"));
 if (NULL==pWelcome)
 {
  return false;
 }
 pMsg->LinkEndChild(pWelcome);
 // 设置Welcome节点的值
 std::string strValue = _T("Welcome to MyApp");
 TiXmlText *pWelcomeValue = new TiXmlText(strValue);
 pWelcome->LinkEndChild(pWelcomeValue);
 // 生成子节点:Farewell
 TiXmlElement *pFarewell = new TiXmlElement(_T("Farewell"));
 if (NULL==pFarewell)
 {
  return false;
 }
 pMsg->LinkEndChild(pFarewell);
 // 设置Farewell节点的值
 strValue = _T("Thank you for using MyApp");
 TiXmlText *pFarewellValue = new TiXmlText(strValue);
 pFarewell->LinkEndChild(pFarewellValue);
 // 生成子节点:Windows
 TiXmlElement *pWindows = new TiXmlElement(_T("Windows"));
 if (NULL==pWindows)
 {
  return false;
 }
 pRootEle->LinkEndChild(pWindows);
 // 生成子节点:Window
 TiXmlElement *pWindow = new TiXmlElement(_T("Window"));
 if (NULL==pWindow)
 {
  return false;
 }
 pWindows->LinkEndChild(pWindow);
    // 设置节点Window的值
    pWindow->SetAttribute(_T("name"),_T("MainFrame"));
    pWindow->SetAttribute(_T("x"),_T("5"));
 pWindow->SetAttribute(_T("y"),_T("15"));
    pWindow->SetAttribute(_T("w"),_T("400"));
    pWindow->SetAttribute(_T("h"),_T("250"));
 // 生成子节点:Window
 TiXmlElement *pConnection  = new TiXmlElement(_T("Connection"));
 if (NULL==pConnection)
 {
  return false;
 }
 pRootEle->LinkEndChild(pConnection);
 // 设置节点Connection的值
 pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));
 pConnection->SetAttribute(_T("timeout"),_T("123.456000"));
    pDoc->SaveFile(XmlFile);
 return true;

 

 

 

       不知你注意到上面的规律没有?首先父节点连接字节点使用函数LinkEndChild,使用方法是:pParentNode-> LinkEndChild(pChild);其次设置类似这种结构<Window name="MainFrame" x="5" y="15" w="400" h="250" />采用SetAttribute函数,这个函数有两个参数,前一个参数表示键,后一个参数表示键值,设置<Farewell>Thank you for using MyApp</Farewell>这种结构采用TiXmlText类,使用LinkEndChild函数进行连结。

 


      上面是创建xml文件的代码,下面介绍读取xml文件的代码。打印整个xml文件的代码很简单,代码如下:

 


view plaincopy to clipboardprint?
 
bool PaintXml(std::string XmlFile)  
 
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    
        return false;  
    
    pDoc->LoadFile(XmlFile);  
    pDoc->Print();  
    return true;  


bool PaintXml(std::string XmlFile)
{
 // 定义一个TiXmlDocument类指针
 TiXmlDocument *pDoc = new TiXmlDocument();
 if (NULL==pDoc)
 {
  return false;
 }
 pDoc->LoadFile(XmlFile);
    pDoc->Print();
 return true;
}
 

 

 

 

   下次介绍使用tinyxml库对xml文件进行查询指定节点、删除指定节点、修改指定节点和增加节点的用法。

参考文献:


1.《TinyXML入门教程 》
2. 《tinyxml 使用笔记与总结 》
3. 《TinyXML Tutorial 中文指南 》


出处:http://blog.csdn.net/clever101/archive/2010/02/28/5334369.aspx

 

 

 

有关TinyXML使用的简单总结

    这次使用了TinyXML后,觉得这个东西真是不错,于是将使用方法坐下总结来和大家分享。
    该解析库在开源网站(http://sourceforge.net )上有下载,在本Blog也提供下载(下载TinyXML
    TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在WindowsLinux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这课XML树。
    注:DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系(理解html语言的读者会很容易理解这种树状模型)。               
    如下是一个XML片段:
    <Persons>
        <Person ID="1">
            <name>周星星</name>
            <age>20</age>
        </Person>
        <Person ID="2">
            <name>白晶晶</name>
            <age>18</age>
        </Person>
    </Persons>
    在TinyXML中,根据XML的各种元素来定义了一些类:
        TiXmlBase:整个TinyXML模型的基类。
                TiXmlAttribute:对应于XML中的元素的属性。
                TiXmlNode:对应于DOM结构中的节点。
                        TiXmlComment:对应于XML中的注释。
                        TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
                        TiXmlDocument:对应于XML的整个文档。
                        TiXmlElement:对应于XML的元素。
                        TiXmlText:对应于XML的文字部分。
                        TiXmlUnknown:对应于XML的未知部分。 
        TiXmlHandler:定义了针对XML的一些操作。
    那我们如何使用这些类以及他们的方法来操纵我们的XML呢?请看下面。
    一、读取XML(假设我们的Xml文档中的内容与上面的Xml内容一样)
    //创建一个XML的文档对象
    TiXmlDocument *myDocument = new TiXmlDocument("填上你的Xml文件名");
    myDocument->LoadFile();
    //获得根元素,即Persons。
    TiXmlElement *RootElement = myDocument.RootElement();
    //输出根元素名称,即输出Persons。
    cout << RootElement->Value() << endl;
    //获得第一个Person节点。
    TiXmlElement *FirstPerson = RootElement->FirstChildElement();
    //获得第一个Person的name节点和age节点和ID属性。
    TiXmlElement *NameElement = FirstPerson->FirstChildElement();
    TiXmlElement *AgeElement = NameElement->NextSiblingElement();
    TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
    //输出第一个Person的name内容,即周星星;age内容,即20;ID属性,即1。
    cout << NameElement->FirstChild()->Value << endl;
    cout << AgeElement->FirstChild()->Value << endl;
    cout << IDAttribute->Value() << endl;

    
    看,读取XML是不是很简单阿,和Java的XML解析库非常的相似,就是名字改了一下而已。
    二、生成XML内容
    //创建一个XML的文档对象。
    TiXmlDocument *myDocument = new TiXmlDocument();
    //创建一个根元素并连接。
    TiXmlElement *RootElement = new TiXmlElement("Persons");
    myDocument->LinkEndChild(RootElement);
    //创建一个Person元素并连接。
    TiXmlElement *PersonElement = new TiXmlElement("Person");
    RootElement->LinkEndChild(PersonElement);
    //设置Person元素的属性。
    PersonElement->SetAttribute("ID", "1");
    //创建name元素、age元素并连接。
    TiXmlElement *NameElement = new TiXmlElement("name");
    TiXmlElement *AgeElement = new TiXmlElement("age");
    PersonElement->LinkEndChild(NameElement);
    PersonElement->LinkEndChild(AgeElement);
    //设置name元素和age元素的内容并连接。
    TiXmlText *NameContent = new TiXmlText("周星星");
    TiXmlText *AgeContent = new TiXmlText("20");
    NameElement->LinkEndChild(NameContent);
    AgeElement->LinkEndChild(AgeContent);
    //保存到文件
    myDocument->SaveFile("要保存的xml文件名");
    这样,便创建了一个如下的xml文件:
    <Persons>
        <Person ID="1">
            <name>周星星</name>
            <age>20</age>
        </Person>
    </Persons>
    
    是不是很简单啊?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值