zoukankan      html  css  js  c++  java
  • XML 总结 参考 查阅

    TinyXml快速入门(一) 》   

    对于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文件的形式:

     

     

    [xhtml] view plaincopy
    1. example1.xml:  
    2. <?xml version="1.0" ?>  
    3. <Hello>World</Hello>  
    4. example2.xml:  
    5. <?xml version="1.0" ?>  
    6. <poetry>  
    7.        <verse>  
    8.                Alas  
    9.                  Great World  
    10.                        Alas (again)  
    11.        </verse>  
    12. </poetry>  
    13. example3.xml:  
    14. <?xml version="1.0" ?>  
    15. <shapes>  
    16.        <circle name="int-based" x="20" y="30" r="50" />  
    17.        <point name="float-based" x="3.5" y="52.1" />  
    18. </shapes>  
    19. example4.xml:  
    20. <?xml version="1.0" ?>  
    21. <MyApp>  
    22.     <Messages>  
    23.         <Welcome>Welcome to MyApp</Welcome>  
    24.         <Farewell>Thank you for using MyApp</Farewell>  
    25.     </Messages>  
    26.     <Windows>  
    27.         <Window name="MainFrame" x="5" y="15" w="400" h="250" />  
    28.     </Windows>  
    29.     <Connection ip="192.168.0.1" timeout="123.456000" />  
    30. </MyApp>  

     

     

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

     

    xml struct


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

     

        

    1. /*! 
    2. *  /brief 创建xml文件。 
    3. * 
    4. *  /param XmlFile xml文件全路径。 
    5. *  /return 是否成功。true为成功,false表示失败。 
    6. */  
    7. bool CreateXml(std::string XmlFile)  
    8. {  
    9.     // 定义一个TiXmlDocument类指针  
    10.     TiXmlDocument *pDoc = new TiXmlDocument;  
    11.     if (NULL==pDoc)  
    12.     {  
    13.         return false;  
    14.     }  
    15.     TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(_T("1.0"),_T(""),_T(""));  
    16.     if (NULL==pDeclaration)  
    17.     {  
    18.         return false;  
    19.     }  
    20.     pDoc->LinkEndChild(pDeclaration);  
    21.     // 生成一个根节点:MyApp  
    22.     TiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));  
    23.     if (NULL==pRootEle)  
    24.     {  
    25.         return false;  
    26.     }  
    27.     pDoc->LinkEndChild(pRootEle);  
    28.     // 生成子节点:Messages  
    29.     TiXmlElement *pMsg = new TiXmlElement(_T("Messages"));  
    30.     if (NULL==pMsg)  
    31.     {  
    32.         return false;  
    33.     }  
    34.     pRootEle->LinkEndChild(pMsg);  
    35.     // 生成子节点:Welcome  
    36.     TiXmlElement *pWelcome = new TiXmlElement(_T("Welcome"));  
    37.     if (NULL==pWelcome)  
    38.     {  
    39.         return false;  
    40.     }  
    41.     pMsg->LinkEndChild(pWelcome);  
    42.     // 设置Welcome节点的值  
    43.     std::string strValue = _T("Welcome to MyApp");  
    44.     TiXmlText *pWelcomeValue = new TiXmlText(strValue);  
    45.     pWelcome->LinkEndChild(pWelcomeValue);  
    46.     // 生成子节点:Farewell  
    47.     TiXmlElement *pFarewell = new TiXmlElement(_T("Farewell"));  
    48.     if (NULL==pFarewell)  
    49.     {  
    50.         return false;  
    51.     }  
    52.     pMsg->LinkEndChild(pFarewell);  
    53.     // 设置Farewell节点的值  
    54.     strValue = _T("Thank you for using MyApp");  
    55.     TiXmlText *pFarewellValue = new TiXmlText(strValue);  
    56.     pFarewell->LinkEndChild(pFarewellValue);  
    57.     // 生成子节点:Windows  
    58.     TiXmlElement *pWindows = new TiXmlElement(_T("Windows"));  
    59.     if (NULL==pWindows)  
    60.     {  
    61.         return false;  
    62.     }  
    63.     pRootEle->LinkEndChild(pWindows);  
    64.     // 生成子节点:Window  
    65.     TiXmlElement *pWindow = new TiXmlElement(_T("Window"));  
    66.     if (NULL==pWindow)  
    67.     {  
    68.         return false;  
    69.     }  
    70.     pWindows->LinkEndChild(pWindow);  
    71.     // 设置节点Window的值  
    72.     pWindow->SetAttribute(_T("name"),_T("MainFrame"));  
    73.     pWindow->SetAttribute(_T("x"),_T("5"));  
    74.     pWindow->SetAttribute(_T("y"),_T("15"));  
    75.     pWindow->SetAttribute(_T("w"),_T("400"));  
    76.     pWindow->SetAttribute(_T("h"),_T("250"));  
    77.     // 生成子节点:Window  
    78.     TiXmlElement *pConnection  = new TiXmlElement(_T("Connection"));  
    79.     if (NULL==pConnection)  
    80.     {  
    81.         return false;  
    82.     }  
    83.     pRootEle->LinkEndChild(pConnection);  
    84.     // 设置节点Connection的值  
    85.     pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));  
    86.     pConnection->SetAttribute(_T("timeout"),_T("123.456000"));  
    87.     pDoc->SaveFile(XmlFile);  
    88.     return true;  
    89. }   

     

     

     

           不知你注意到上面的规律没有?首先父节点连接字节点使用函数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函数进行连结。

     TinyXml快速入门(二)

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

     

     

    1. /*! 
    2. *  /brief 打印xml文件。 
    3. * 
    4. *  /param XmlFile xml文件全路径。 
    5. *  /return 是否成功。true为成功,false表示失败。 
    6. */  
    7. bool PaintXml(std::string XmlFile)  
    8. {  
    9.     // 定义一个TiXmlDocument类指针  
    10.     TiXmlDocument *pDoc = new TiXmlDocument();  
    11.     if (NULL==pDoc)  
    12.     {  
    13.         return false;  
    14.     }  
    15.     pDoc->LoadFile(XmlFile);  
    16.     pDoc->Print();  
    17.     return true;  
    18. }  

     

    在《TinyXml快速入门(一) 》中我介绍了使用TinyXml库如何创建和打印xml文件,下面我介绍使用tinyxml库对xml文件进行一系列的操作,包括获取xml文件声明,查询指定节点、删除指定节点、修改指定节点和增加节点的用法。在《TinyXml快速入门(一)》中我们知道xml文件中的一个节点元素实际包含两种值:属性和文本。其中属性在我看来可以看作是STL中的map,一个属性带一个属性值,map中也是一个键带一个键值。因此查询指定节点、删除指定节点和增加节点必然是需要实现两种方法,删除指定节点只需要实现一种方法。鉴于内容较多,在本文中介绍获取xml文件声明,查询指定节点、删除指定节点的做法,修改指定节点和增加节点的做法在后续的文章介绍。

     

          首先是获取xml文件声明。xml文件声明包括三方面的内容:Version、Standalone和Encoding。其源码如下:

     

     

    1. /*! 
    2. *  /brief 获取xml文件的声明。 
    3. * 
    4. *  /param XmlFile xml文件全路径。 
    5. *  /param strVersion  Version属性值 
    6. *  /param strStandalone Standalone属性值 
    7. *  /param strEncoding Encoding属性值 
    8. *  /return 是否成功。true为成功,false表示失败。 
    9. */  
    10. bool GetXmlDeclare(std::string XmlFile,  
    11.                    std::string &strVersion,  
    12.                    std::string &strStandalone,  
    13.                    std::string &strEncoding)  
    14. {  
    15.     // 定义一个TiXmlDocument类指针  
    16.     TiXmlDocument *pDoc = new TiXmlDocument();  
    17.     if (NULL==pDoc)  
    18.     {  
    19.         return false;  
    20.     }  
    21.     pDoc->LoadFile(XmlFile);  
    22.       TiXmlNode* pXmlFirst = pDoc->FirstChild();     
    23.       if (NULL != pXmlFirst)    
    24.      {    
    25.           TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration();    
    26.           if (NULL != pXmlDec)    
    27.           {    
    28.               strVersion = pXmlDec->Version();  
    29.               strStandalone = pXmlDec->Standalone();  
    30.               strEncoding = pXmlDec->Encoding();  
    31.           }  
    32.       }  
    33.       return true;  
    34. }  

     

     

          我们发现无论查询节点、删除节点、修改节点和增加节点,其实都离不开一个函数,就是根据节点名获取相关节点指针。那么我们就先实现一个根据节点名获取节点指针的函数:

     

         

    1. /*! 
    2. *  /brief 通过根节点和节点名获取节点指针。 
    3. * 
    4. *  /param pRootEle   xml文件的根节点。 
    5. *  /param strNodeName  要查询的节点名 
    6. *  /param Node      需要查询的节点指针 
    7. *  /return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。 
    8. */  
    9. bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)  
    10. {  
    11.      // 假如等于根节点名,就退出  
    12.      if (strNodeName==pRootEle->Value())  
    13.      {  
    14.          Node = pRootEle;  
    15.          return true;  
    16.      }  
    17.       TiXmlElement* pEle = pRootEle;    
    18.       for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())    
    19.     {    
    20.           //递归处理子节点,获取节点指针  
    21.           if(GetNodePointerByName(pEle,strNodeName,Node))  
    22.               return true;  
    23.      }    
    24.      return false;  
    25. }   

     

     

           有了这个函数,我们就很容易实现查询节点的相应文本或属性值。

     

     

    1.   /*! 
    2. *  /brief 通过节点查询。 
    3. * 
    4. *  /param XmlFile   xml文件全路径。 
    5. *  /param strNodeName  要查询的节点名 
    6. *  /param strText      要查询的节点文本 
    7. *  /return 是否成功。true为成功,false表示失败。 
    8. */  
    9. bool QueryNode_Text(std::string XmlFile,std::string strNodeName,std::string &strText)  
    10. {  
    11.     // 定义一个TiXmlDocument类指针  
    12.     TiXmlDocument *pDoc = new TiXmlDocument();  
    13.     if (NULL==pDoc)  
    14.     {  
    15.         return false;  
    16.     }  
    17.     pDoc->LoadFile(XmlFile);  
    18.     TiXmlElement *pRootEle = pDoc->RootElement();  
    19.     if (NULL==pRootEle)  
    20.     {  
    21.         return false;  
    22.     }  
    23.    TiXmlElement *pNode = NULL;  
    24.    GetNodePointerByName(pRootEle,strNodeName,pNode);  
    25.    if (NULL!=pNode)  
    26.    {  
    27.         strText = pNode->GetText();   
    28.         return true;  
    29.    }  
    30.    else  
    31.    {  
    32.         return false;  
    33.    }  
    34.       
    35. }  
    36. /*! 
    37. *  /brief 通过节点查询。 
    38. * 
    39. *  /param XmlFile   xml文件全路径。 
    40. *  /param strNodeName  要查询的节点名 
    41. *  /param AttMap      要查询的属性值,这是一个map,前一个为属性名,后一个为属性值 
    42. *  /return 是否成功。true为成功,false表示失败。 
    43. */  
    44. bool QueryNode_Attribute(std::string XmlFile,std::string strNodeName,std::map<std::string,std::string> &AttMap)  
    45. {  
    46.     // 定义一个TiXmlDocument类指针  
    47.     typedef std::pair <std::string,std::string> String_Pair;  
    48.     TiXmlDocument *pDoc = new TiXmlDocument();  
    49.     if (NULL==pDoc)  
    50.     {  
    51.         return false;  
    52.     }  
    53.     pDoc->LoadFile(XmlFile);  
    54.     TiXmlElement *pRootEle = pDoc->RootElement();  
    55.     if (NULL==pRootEle)  
    56.     {  
    57.         return false;  
    58.     }  
    59.     TiXmlElement *pNode = NULL;  
    60.     GetNodePointerByName(pRootEle,strNodeName,pNode);  
    61.     if (NULL!=pNode)  
    62.     {  
    63.         TiXmlAttribute* pAttr = NULL;   
    64.         for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())    
    65.         {    
    66.             std::string strAttName = pAttr->Name();  
    67.             std::string strAttValue = pAttr->Value();  
    68.             AttMap.insert(String_Pair(strAttName,strAttValue));  
    69.         }    
    70.         return true;  
    71.     }  
    72.     else  
    73.     {  
    74.         return false;  
    75.     }  
    76.     return true;  
    77. }  

     

     

       下面是删除指定节点的函数,其中考虑了删除根节点的情况:

     

     

    1. /*! 
    2. *  /brief 删除指定节点的值。 
    3. * 
    4. *  /param XmlFile xml文件全路径。 
    5. *  /param strNodeName 指定的节点名。 
    6. *  /return 是否成功。true为成功,false表示失败。 
    7. */  
    8. bool DelNode(std::string XmlFile,std::string strNodeName)  
    9. {  
    10.     // 定义一个TiXmlDocument类指针  
    11.     TiXmlDocument *pDoc = new TiXmlDocument();  
    12.     if (NULL==pDoc)  
    13.     {  
    14.         return false;  
    15.     }  
    16.     pDoc->LoadFile(XmlFile);  
    17.     TiXmlElement *pRootEle = pDoc->RootElement();  
    18.     if (NULL==pRootEle)  
    19.     {  
    20.         return false;  
    21.     }  
    22.     TiXmlElement *pNode = NULL;  
    23.     GetNodePointerByName(pRootEle,strNodeName,pNode);  
    24.     // 假如是根节点  
    25.     if (pRootEle==pNode)  
    26.     {  
    27.           if(pDoc->RemoveChild(pRootEle))  
    28.           {  
    29.                pDoc->SaveFile(XmlFile);  
    30.                return true;  
    31.           }  
    32.           else   
    33.               return false;  
    34.     }  
    35.     // 假如是其它节点  
    36.     if (NULL!=pNode)  
    37.     {  
    38.         TiXmlNode *pParNode =  pNode->Parent();  
    39.         if (NULL==pParNode)  
    40.         {  
    41.                return false;  
    42.         }  
    43.               
    44.         TiXmlElement* pParentEle = pParNode->ToElement();  
    45.         if (NULL!=pParentEle)  
    46.         {  
    47.             if(pParentEle->RemoveChild(pNode))  
    48.                  pDoc->SaveFile(XmlFile);  
    49.             else  
    50.                 return false;  
    51.         }  
    52.     }  
    53.     else  
    54.     {  
    55.           return false;  
    56.     }  
    57.      return false;  
    58. }  

     

  • 相关阅读:
    Node.js入门 Hello World
    Select自动下拉实现
    js从url截取参数(简写)
    如何关闭SQL Server受影响行数
    适用于多种查询结果集的分页(不要存储过程,简单易懂)
    单条件存储过程分页(SQL Server)&WS调用(只是其中一种 实现的方法还有很多)
    Simple Package Tool 学习
    Python try/except/finally等
    Python os.path模块
    《UVM实战》代码示例
  • 原文地址:https://www.cnblogs.com/cy568searchx/p/2957484.html
Copyright © 2011-2022 走看看