下载:

官方文档:http://www.grinninglizard.com/tinyxmldocs/index.html

中文翻译:http://www.cnblogs.com/kex1n/archive/2010/10/03/1841502.html

TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。

引用来自tinyXML文档

tinyxml安装和使用

iXmlBase是所有类的基类,TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,其中TiXmlNode类指的是所有被<...>...<.../>包括的内容,而xml中的节点又具体分为以下几方面内容,分别是声明、注释、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown,分别用来指明具体是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号里面的内容,像<... ***=...>,其中***就是一个属性。

 

     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文件的代码:

/*!
*  /brief 创建xml文件。
*
*  /param XmlFile xml文件全路径。
*  /return 是否成功。true为成功,false表示失败。
*/
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;
}&nbsp;
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-18
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2021-12-08
  • 2021-12-19
  • 2022-01-23
  • 2021-06-15
  • 2021-07-23
  • 2021-09-12
  • 2021-07-28
相关资源
相似解决方案