TinyXml快速入门

作者:朱金灿

来源:http://www.cnblogs.com/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文件的形式:

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使用了两种编译选择:使用标准Cchar *类型或者使用STL中的std::string,其中使用预处理器TIXML_USE_STL进行控制,即添加了TIXML_USE_STL为使用std::string的。鉴于STL的广泛使用以及其强大功能,下面我以使用std::stringtinyxml说明。

首先使用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;
}
复制代码

 不知你注意到上面的规律没有?首先父节点连接字节点使用函数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文件的代码很简单,代码如下:

复制代码
/*!
*  \brief 打印xml文件。
*
*  \param XmlFile xml文件全路径。
*  \return 是否成功。true为成功,false表示失败。
*/
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 中文指南

4.       使用TinyXml解析Xml示例

 

 

 

作者:朱金灿

来源:http://www.cnblogs.com/clever101

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

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

复制代码
/*!
*  \brief 获取xml文件的声明。
*
*  \param XmlFile xml文件全路径。
*  \param strVersion  Version属性值
*  \param strStandalone Standalone属性值
*  \param strEncoding Encoding属性值
*  \return 是否成功。true为成功,false表示失败。
*/
bool GetXmlDeclare(std::string XmlFile,
                   std::
string &strVersion,
                   std::
string &strStandalone,
                   std::
string &strEncoding)
{
    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);

      TiXmlNode
* pXmlFirst = pDoc->FirstChild();   
      
if (NULL != pXmlFirst)  
     {  
          TiXmlDeclaration
* pXmlDec = pXmlFirst->ToDeclaration();  
          
if (NULL != pXmlDec)  
          {  
              strVersion 
= pXmlDec->Version();
              strStandalone 
= pXmlDec->Standalone();
              strEncoding 
= pXmlDec->Encoding();
          }
      }
      
return true;
}
复制代码

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

复制代码
/*!
*  \brief 通过根节点和节点名获取节点指针。
*
*  \param pRootEle   xml文件的根节点。
*  \param strNodeName  要查询的节点名
*  \param Node      需要查询的节点指针
*  \return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。
*/
bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)
{
     
// 假如等于根节点名,就退出
     if (strNodeName==pRootEle->Value())
     {
         Node 
= pRootEle;
         
return true;
     }

      TiXmlElement
* pEle = pRootEle;  
      
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())  
    {  
          
//递归处理子节点,获取节点指针
          if(GetNodePointerByName(pEle,strNodeName,Node))
              
return true;
     }  

     
return false;
}
复制代码

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

复制代码
    /*!
*  \brief 通过节点查询。
*
*  \param XmlFile   xml文件全路径。
*  \param strNodeName  要查询的节点名
*  \param strText      要查询的节点文本
*  \return 是否成功。true为成功,false表示失败。
*/
bool QueryNode_Text(std::string XmlFile,std::string strNodeName,std::string &strText)
{
    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

   TiXmlElement 
*pNode = NULL;
   GetNodePointerByName(pRootEle,strNodeName,pNode);
   
if (NULL!=pNode)
   {
        
const char* psz = pNode->GetText(); 
       
if (NULL==psz)
       {
        strText 
= _T("");
        }
       
else
       {
         strText 
= psz;
        }
        
return true;
   }
   
else
   {
        
return false;
   }
    
}


/*!
*  \brief 通过节点查询。
*
*  \param XmlFile   xml文件全路径。
*  \param strNodeName  要查询的节点名
*  \param AttMap      要查询的属性值,这是一个map,前一个为属性名,后一个为属性值
*  \return 是否成功。true为成功,false表示失败。
*/
bool QueryNode_Attribute(std::string XmlFile,std::string strNodeName,std::map<std::string,std::string> &AttMap)
{
    
// 定义一个TiXmlDocument类指针
    typedef std::pair <std::string,std::string> String_Pair;

    TiXmlDocument 
*pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;

    GetNodePointerByName(pRootEle,strNodeName,pNode);

    
if (NULL!=pNode)
    {
        TiXmlAttribute
* pAttr = NULL; 
        
for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
        {  
            std::
string strAttName = pAttr->Name();
            std::
string strAttValue = pAttr->Value();
            AttMap.insert(String_Pair(strAttName,strAttValue));
        }  
        
return true;
    }
    
else
    {
        
return false;
    }
    
return true;
}
复制代码

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

复制代码
/*!
*  \brief 删除指定节点的值。
*
*  \param XmlFile xml文件全路径。
*  \param strNodeName 指定的节点名。
*  \return 是否成功。true为成功,false表示失败。
*/
bool DelNode(std::string XmlFile,std::string strNodeName)
{
    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;

    GetNodePointerByName(pRootEle,strNodeName,pNode);

    
// 假如是根节点
    if (pRootEle==pNode)
    {
          
if(pDoc->RemoveChild(pRootEle))
          {
               pDoc
->SaveFile(XmlFile);
               
return true;
          }
          
else 
              
return false;
    }

    
// 假如是其它节点
    if (NULL!=pNode)
    {
        TiXmlNode 
*pParNode =  pNode->Parent();
        
if (NULL==pParNode)
        {
               
return false;
        }
            
        TiXmlElement
* pParentEle = pParNode->ToElement();
        
if (NULL!=pParentEle)
        {
            
if(pParentEle->RemoveChild(pNode))
                 pDoc
->SaveFile(XmlFile);
            
else
                
return false;
        }
    }
    
else
    {
          
return false;
    }

     
return false;
}


复制代码

作者:朱金灿

来源:http://www.cnblogs.com/clever101

      在《TinyXml 快速入门(二)》介绍使用tinyxml库获取xml文件声明,查询指定节点、删除指定节点的做法。在本文中继续介绍修改指定节点和增加节点的做法。

     修改节点其实和查询指定节点的值有点类似,也分为两个函数,一个实现修改文本。另一个负责修改属性。

复制代码
/*!
*  \brief 修改指定节点的文本。
*
*  \param XmlFile xml文件全路径。
*  \param strNodeName 指定的节点名。
*  \param strText 重新设定的文本的值
*  \return 是否成功。true为成功,false表示失败。
*/
bool ModifyNode_Text(const std::string& XmlFile,const std::string& strNodeName,const std::string& strText)
{
    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;

    GetNodePointerByName(pRootEle,strNodeName,pNode);

    
if (NULL!=pNode)
    {
        pNode
->Clear();  // 首先清除所有文本
        
// 然后插入文本,保存文件
        TiXmlText *pValue = new TiXmlText(strText);
        pNode
->LinkEndChild(pValue);
        pDoc
->SaveFile(XmlFile);
        
return true;
    }
    
else
        
return false;
}


/*!
*  \brief 修改指定节点的属性值。
*
*  \param XmlFile xml文件全路径。
*  \param strNodeName 指定的节点名。
*  \param AttMap 重新设定的属性值,这是一个map,前一个为属性名,后一个为属性值
*  \return 是否成功。true为成功,false表示失败。
*/
bool ModifyNode_Attribute(const std::string& XmlFile,const std::string& strNodeName,
                 
const std::map<std::string,std::string> &AttMap)
{
    typedef std::pair 
<std::string,std::string> String_Pair;

    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }
 
    TiXmlElement 
*pNode = NULL;
    GetNodePointerByName(pRootEle,strNodeName,pNode);

    
if (NULL!=pNode)
    {
        TiXmlAttribute
* pAttr = NULL; 
        std::
string strAttName = _T("");
        std::
string strAttValue = _T("");
        
for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
        {  
            strAttName 
= pAttr->Name();

            std::map
<std::string,std::string>::const_iterator iter;
            
for (iter=AttMap.begin();iter!=AttMap.end();iter++)
            {
                
if (strAttName==iter->first)
                {
                    pAttr
->SetValue(iter->second);
                }
            }

        }  
        pDoc
->SaveFile(XmlFile);
        
return true;
    }
    
else
    {
        
return false;
    }

}
复制代码

对于ModifyNode_Attribute函数,这里稍微介绍一下如何使用,比如对于下面这样一个xml文件:

复制代码
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<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>
复制代码

我们如果要修改节点的Connection的ip为192.168.0.100,timeout为1000,我们可以这样用:

复制代码
    std::string XmlFile = _T("E:\\TestTinyxml\\example4.xml");
    std::
string strNodeName = _T("Connection");
   typedef std::pair 
<std::string,std::string> String_Pair;
   std::map
<std::string,std::string> AttMap;
   AttMap.insert(String_Pair(_T(
"ip"),_T("192.168.0.100")));
   AttMap.insert(String_Pair(_T(
"timeout"),_T("1000")));
   ModifyNode_Attribute(XmlFile,strNodeName,AttMap);
复制代码

下面是增加节点的两个函数:

复制代码
/*!
*  \brief 增加指定节点的文本。
*
*  \param XmlFile xml文件全路径。
*  \param strParNodeName 要增加的节点的父节点。
*  \param strNodeName 指定的节点名。
*  \param strText 要增加的文本
*  \return 是否成功。true为成功,false表示失败。
*/
bool AddNode_Text(const std::string& XmlFile,const std::string& strParNodeName,const std::string& strNodeName,const std::string& strText)
{
    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;
    GetNodePointerByName(pRootEle,strParNodeName,pNode);

    
if (NULL!=pNode)
    {
        
// 生成子节点:pNewNode
        TiXmlElement *pNewNode = new TiXmlElement(strNodeName);
        
if (NULL==pNewNode)
        {
            
return false;
        }
        
// 设置节点文本,然后插入节点
        TiXmlText *pNewValue = new TiXmlText(strText);
        pNewNode
->LinkEndChild(pNewValue);

        pNode
->InsertEndChild(*pNewNode);
        pDoc
->SaveFile(XmlFile);
        
return true;
    }
    
else
         
return false;
    
}

/*!
*  \brief 增加节点。
*
*  \param XmlFile xml文件全路径。
*  \param strParNodeName 要增加的节点的父节点。
*  \param strNodeName 指定的节点名。
*  \param AttMap 要增加的节点设定的属性值,这是一个map,前一个为属性名,后一个为属性值
*  \return 是否成功。true为成功,false表示失败。
*/
bool AddNode_Attribute(const std::string& XmlFile,const std::string& strParNodeName,const std::string strNodeName,const std::map<std::string,std::string> &AttMap)
{
    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;
    GetNodePointerByName(pRootEle,strParNodeName,pNode);

    
if (NULL!=pNode)
    {
        
// 生成子节点:pNewNode
        TiXmlElement *pNewNode = new TiXmlElement(strNodeName);
        
if (NULL==pNewNode)
        {
            
return false;
        }
        
// 设置节点的属性值,然后插入节点
        std::map<std::string,std::string>::const_iterator iter;
        
for (iter=AttMap.begin();iter!=AttMap.end();iter++)
        {
             pNewNode
->SetAttribute(iter->first,iter->second);
        }

        pNode
->InsertEndChild(*pNewNode);
        pDoc
->SaveFile(XmlFile);
        
return true;
    }
    
else
        
return false;
}
复制代码

代码修改记录: 2010.04.08.在上面各个函数的参数增加const修饰符。

 

      至此tinyxml入门文章全部完成,相关源码下载请访问:

 Tinyxml库及测试源码下载

 

 

作者:朱金灿

来源:http://www.cnblogs.com/clever101

       一位网友说看了我的TinyXml快速入门系列文章。在修改节点属性值函数ModifyNode_Attribute这个函数的用法参照我的例子运行了下,

<Connection ip="192.168.0.1" timeout="123.456000"/> 如果只有这一个节点的时候是修改正确的,但是如果再加上几个相同名字的节点呢?就像下面一样:

<Connection ip="192.168.0.1" timeout="123.456000"/>
<Connection ip="192.168.0.2" timeout="123.456001"/>
<Connection ip="192.168.0.3" timeout="123.456002"/>

    现在他想修改的是<Connection ip="192.168.0.3" timeout="123.456002"/> 这行为<Connection ip="192.168.0.4" timeout="123.456003"/>

请问该如何修改?

     鉴于他所提问题有一定代表性,我就再写一篇以作答复。在《TinyXml快速入门(二)》中我提到了无论查询节点、删除节点、修改节点和增加节点,其实都离不开一个函数,就是根据节点名获取相关节点指针,其中一个关键的函数:

复制代码
/*!
*  \brief 通过根节点和节点名获取节点指针。
*
*  \param pRootEle   xml文件的根节点。
*  \param strNodeName  要查询的节点名
*  \param Node      需要查询的节点指针
*  \return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。
*/
bool GetNodePointerByName(TiXmlElement* pRootEle,const std::string &strNodeName,TiXmlElement* &Node)
{
     
// 假如等于根节点名,就退出
     if (strNodeName==pRootEle->Value())
     {
         Node 
= pRootEle;
         
return true;
     }

      TiXmlElement
* pEle = pRootEle;  
      
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())  
    {  
          
//递归处理子节点 
          if(GetNodePointerByName(pEle,strNodeName,Node))
              
return true;
     }  

     
return false;
}
复制代码

    具体到这位网友的问题,其实就是增加了一个判断条件,就是要求所求节点不但要求节点名为Connection,还有属性ip的值为192.168.0.3。那么我们可以对GetNodePointerByName函数稍作修改:

复制代码

   
/*!
*  \brief 通过根节点和节点名以及节点的一个属性值获取节点指针。
*
*  \param pRootEle   xml文件的根节点。
*  \param strNodeName  要查询的节点名
*  \param strNodeName  要查询的节点的一个属性值
*  \param Node      需要查询的节点指针
*  \return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。
*/
bool GetNodePointerByName_Attribute(TiXmlElement* pRootEle,
                                    
const std::string &strNodeName,
                                    
const std::string &strAttributeValue,
                                    TiXmlElement
* &Node)
{
    assert(NULL
!=pRootEle);
    
// 假如等于根节点名,就退出
    if (strNodeName==pRootEle->Value())
    {

        TiXmlAttribute
* pAttr = NULL; 
        
for (pAttr = pRootEle->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
        {  
            std::
string strAttValue = pAttr->Value();
            
if (strAttributeValue==strAttValue)
            {
                Node 
= pRootEle;
            }
        }  

    }

    TiXmlElement
* pEle = pRootEle;  
    
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())  
    {  
        
//递归处理子节点 
        if(GetNodePointerByName_Attribute(pEle,strNodeName,strAttributeValue,Node))
            
return true;
    }  

    
return false;
}
复制代码

然后再增加一个修改属性的函数:

复制代码

/*!
*  \brief 修改指定节点的属性。
*
*  \param XmlFile xml文件全路径。
*  \param strNodeName 指定的节点名。
*  \param strAttValue 指定的节点的其中一个属性值。
*  \param AttMap 重新设定的属性值,这是一个map,前一个为属性名,后一个为属性值
*  \return 是否成功。true为成功,false表示失败。
*/
bool ModifyNode_Attribute2(const std::string& XmlFile,const std::string& strNodeName,
                          
const std::string& strAttValue,
                          
const std::map<std::string,std::string> &AttMap)
{
    typedef std::pair 
<std::string,std::string> String_Pair;

    
// 定义一个TiXmlDocument类指针
    TiXmlDocument *pDoc = new TiXmlDocument();
    
if (NULL==pDoc)
    {
        
return false;
    }

    pDoc
->LoadFile(XmlFile);
    TiXmlElement 
*pRootEle = pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;
    GetNodePointerByName_Attribute(pRootEle,strNodeName,strAttValue,pNode);

    
if (NULL!=pNode)
    {
        TiXmlAttribute
* pAttr = NULL; 
        std::
string strAttName = _T("");
        std::
string strAttValue = _T("");
        
for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
        {  
            strAttName 
= pAttr->Name();

            std::map
<std::string,std::string>::const_iterator iter;
            
for (iter=AttMap.begin();iter!=AttMap.end();iter++)
            {
                
if (strAttName==iter->first)
                {
                    pAttr
->SetValue(iter->second);
                }
            }

        }  
        pDoc
->SaveFile(XmlFile);
        
return true;
    }
    
else
    {
        
return false;
    }

}
复制代码

如果要将

<Connection ip="192.168.0.1" timeout="123.456000"/>

<Connection ip="192.168.0.2" timeout="123.456001"/>

<Connection ip="192.168.0.3" timeout="123.456002"/>

   中的<Connection ip="192.168.0.3" timeout="123.456002"/> 这行修改为<Connection ip="192.168.0.4" timeout="123.456003"/>

     那么测试代码就如下:

复制代码
   std::string strNodeName = _T("Connection");
   std::
string strAttValue = _T("192.168.0.3");
  typedef std::pair 
<std::string,std::string> String_Pair;
   std::map
<std::string,std::string> AttMap;
   AttMap.insert(String_Pair(_T(
"ip"),_T("192.168.0.4")));
   AttMap.insert(String_Pair(_T(
"timeout"),_T("123.456003")));
   ModifyNode_Attribute2(XmlFile,strNodeName,strAttValue,AttMap);
复制代码

作者:朱金灿

来源:http://www.cnblogs.com/clever101

在TinyXml快速入门的系列文章中(详情见本博客),我只是将tinyxml类库解析xml文件的类封装为API接口。这次我决定将这些API接口对象化,并结合自定义的数据结构解析xml文件。

     具体是新建一个CXmlParse类,头文件声明如下:

复制代码
#include <string>
#include 
<vector>
#include 
<map>
#include 
"..\tinyxml\tinyxml.h"
#include 
"BaseStruct.h"
using std::string;
using std::vector;
using std::map;


/*! \struct MyAppInfo XmlParse.h 
* \brief 程序信息结构体.
*
*   包含了程序名、公司名和公司网址
*/
struct MyAppInfo 
{
     MyAppInfo()
     {
         m_strAppName 
= _T("");
         m_strCompanyName  
= _T("") ;
         m_strUrl  
= _T("") ;
     }

    
string m_strAppName;
    
string m_strCompanyName;
    
string m_strUrl;
};
/*! \class CXmlParse XmlParse.h 
*  \brief xml文件解析类
*
*    实现对xml文件的查询、修改和删除节点操作
*  \author 朱金灿.
*  \version 0.1
*  \date    2010.03.28.
*/
class CXmlParse
{
public:
    CXmlParse(
void);
    
~CXmlParse(void);

public:

    
/*!
    *  \brief 打开xml文件。
    *
    *  \param [in]XmlFile xml文件全路径。
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool OpenXml(const string& XmlFile);

    
/*!
    *  \brief 在控制台上打印xml文件。
    *
    *  \return 无。
    
*/
    
void PaintXml();

    
/*!
    *  \brief 获取xml文件的声明。
    *
    *  \param strVersion  [in][out]Version属性值
    *  \param strStandalone [in][out]Standalone属性值
    *  \param strEncoding [in][out]Encoding属性值
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool GetXmlDeclare(string &strVersion,string &strStandalone,string &strEncoding);

    
/*!
    *  \brief 通过节点查询。
    *
    *  \param strNodeName  [in]要查询的节点名
    *  \param strText      [in][out]查询的结果节点文本
    *  \return 是否找到。true为成功找到,false表示没有找到。
    
*/
    
bool QueryNode_Text(const string& strNodeName,string &strText);

    
/*!
    *  \brief 通过节点查询。
    *
    *  \param strNodeName  [in]要查询的节点名
    *  \param AttMap      [in][out]查询的结果属性值,这是一个map,前一个为属性名,后一个为属性值
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool QueryNode_Attribute(const string& strNodeName,map<string,string> &AttMap);

    
/*!
    *  \brief 删除指定节点的值。
    *
    *  \param strNodeName [in]指定的节点名。
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool DelNode(const string& strNodeName);

    
/*!
    *  \brief 修改指定节点的文本。
    *
    *  \param strNodeName [in]指定的节点名。
    *  \param strText [in]重新设定的文本的值
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool ModifyNode_Text(const string& strNodeName,const string& strText);

    
/*!
    *  \brief 修改指定节点的属性。
    *
    *  \param [in]strNodeName 指定的节点名。
    *  \param [in]AttMap 重新设定的属性值,这是一个map,前一个为属性名,后一个为属性值
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool ModifyNode_Attribute(const string& strNodeName,
          
const map<string,string>& AttMap);

    
/*!
    *  \brief 修改指定节点的属性。
    *
    *  \param [in]strNodeName 指定的节点名。
    *  \param [in]strAttValue 指定的节点的其中一个属性值。
    *  \param [in]AttMap 重新设定的属性值,这是一个map,前一个为属性名,后一个为属性值
    *  \return 是否成功。true为成功,false表示失败。
    
*/
    
bool ModifyNode_Attribute2(string strNodeName,string strAttValue,
        
const map<string,string> &AttMap);

public:

    
/*!
    *  \brief 获取应用程序信息。
    *
    *  \param [in][out]Info 指定的节点名。
    *  \return 是否成功。true为成功,false表示失败。
    
*/
   
bool GetAppInfo(MyAppInfo& Info);

private:

    
/*!
    *  \brief 通过根节点和节点名获取节点指针。
    *
    *  \param pRootEle   [in]xml文件的待检查的节点。
    *  \param strNodeName  [in]要查询的节点名。
    *  \param Node      [in][out]需要查询的节点指针。
    *  \return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。
    
*/
   
bool GetNodePointerByName(TiXmlElement* pRootEle,const string &strNodeName,TiXmlElement* &Node);

   
/*!
   *  \brief 通过根节点和节点名以及节点的一个属性值获取节点指针。
   *
   *  \param pRootEle   [in]xml文件的待检查的节点。
   *  \param strNodeName  [in]要查询的节点名
   *  \param strNodeName  [in]要查询的节点的一个属性值
   *  \param Node      [in][out]需要查询的节点指针
   *  \return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。
   
*/
   
bool GetNodePointerByName_Attribute(TiXmlElement* pRootEle,
       
const string &strNodeName,
       
const string &strAttributeValue,
       TiXmlElement
* &Node);

protected:

    
/**
    * \brief 实际操作xml文件的类。
    
*/
     TiXmlDocument 
*m_pDoc;

     
/**
     * \brief xml文件全路径。
     
*/
     
string m_strXmlFile;
};
复制代码

实现文件的代码如下:

复制代码
#include <assert.h>
#include 
"XmlParse.h"

CXmlParse::CXmlParse(
void)
{
    m_pDoc 
= NULL;
    m_strXmlFile 
= _T("");
}

CXmlParse::
~CXmlParse(void)
{
    delete m_pDoc;
}

bool CXmlParse::OpenXml(const string& XmlFile)
{
     
if (NULL!=m_pDoc)
     {
         delete m_pDoc;
         m_pDoc 
= NULL;
     }

    m_pDoc 
= new TiXmlDocument();

     
if (NULL==m_pDoc)
     {
         
return false;
     }

     m_pDoc
->LoadFile(XmlFile);
     m_strXmlFile 
= XmlFile;
     
return true;
}

void CXmlParse::PaintXml()
{
     assert(NULL
!=m_pDoc);
     m_pDoc
->Print();
}

bool CXmlParse::GetXmlDeclare(string &strVersion,string &strStandalone,string &strEncoding)
{
     assert(NULL
!=m_pDoc);
     
// 找到第一个节点
     TiXmlNode* pXmlFirst = m_pDoc->FirstChild();   
     
if (NULL != pXmlFirst)  
     {  
         TiXmlDeclaration
* pXmlDec = pXmlFirst->ToDeclaration();  
         
if (NULL != pXmlDec)  
         {  
             
// 获取各种信息
             strVersion = pXmlDec->Version();
             strStandalone 
= pXmlDec->Standalone();
             strEncoding 
= pXmlDec->Encoding();
         }
     }
     
return true;
}

bool CXmlParse::GetNodePointerByName(TiXmlElement* pRootEle,const string &strNodeName,TiXmlElement* &Node)
{
    assert(NULL
!=pRootEle);
    
if (strNodeName==pRootEle->Value())
    {
        Node 
= pRootEle;
        
return true;
    }

    TiXmlElement
* pEle = pRootEle;  
    
for (pEle = pRootEle->FirstChildElement();pEle;pEle = pEle->NextSiblingElement())  
    {  
        
//递归处理子节点 
        if(GetNodePointerByName(pEle,strNodeName,Node))
            
return true;
    }  

    
return false;
}

bool CXmlParse::GetNodePointerByName_Attribute(TiXmlElement* pRootEle,
                                    
const string &strNodeName,
                                    
const string &strAttributeValue,
                                    TiXmlElement
* &Node)
{
    assert(NULL
!=pRootEle);
    
// 假如等于根节点名,就退出
    if (strNodeName==pRootEle->Value())
    {

        TiXmlAttribute
* pAttr = NULL; 
        
for (pAttr = pRootEle->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
        {  
            std::
string strAttValue = pAttr->Value();
            
if (strAttributeValue==strAttValue)
            {
                Node 
= pRootEle;
            }
        }  

    }

    TiXmlElement
* pEle = pRootEle;  
    
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())  
    {  
        
//递归处理子节点 
        if(GetNodePointerByName_Attribute(pEle,strNodeName,strAttributeValue,Node))
            
return true;
    }  
    
return false;
}

 
bool CXmlParse::QueryNode_Text(const string& strNodeName,string &strText)
 {
     assert(NULL
!=m_pDoc);
     TiXmlElement 
*pRootEle = m_pDoc->RootElement();
     
if (NULL==pRootEle)
     {
         
return false;
     }

     TiXmlElement 
*pNode = NULL;

     GetNodePointerByName(pRootEle,strNodeName,pNode);

     
if (NULL!=pNode)
     {
         
const char* psz = pNode->GetText();
         
if (NULL==psz)
         {
             strText 
= _T("");
         }
         
else
         {
             strText 
= psz;
         }
         
return true;
     }
     
else
     {
         
return false;
     }
 }

 
bool CXmlParse::QueryNode_Attribute(const string& strNodeName,map<string,string> &AttMap)
 {
    assert(NULL
!=m_pDoc);
    typedef std::pair 
<std::string,std::string> String_Pair;
    TiXmlElement 
*pRootEle = m_pDoc->RootElement();
    
if (NULL==pRootEle)
    {
        
return false;
    }

    TiXmlElement 
*pNode = NULL;

    GetNodePointerByName(pRootEle,strNodeName,pNode);

    
if (NULL!=pNode)
    {
        TiXmlAttribute
* pAttr = NULL; 
        
for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
        {  
            std::
string strAttName = pAttr->Name();
            std::
string strAttValue = pAttr->Value();
            AttMap.insert(String_Pair(strAttName,strAttValue));
        }  
        
return true;
    }
    
else
    {
        
return false;
    }
}

 
bool CXmlParse::DelNode(const string& strNodeName)
 {
     assert(NULL
!=m_pDoc);
     TiXmlElement 
*pRootEle = m_pDoc->RootElement();
     
if (NULL==pRootEle)
     {
         
return false;
     }

     TiXmlElement 
*pNode = NULL;

     GetNodePointerByName(pRootEle,strNodeName,pNode);

     
// 假如要删除的是根节点
     if (pRootEle==pNode)
     {
         
if(m_pDoc->RemoveChild(pRootEle))
         {
             m_pDoc
->SaveFile(m_strXmlFile);
             
return true;
         }
         
else 
             
return false;
     }

     
// 假如要删除的是其它节点
     if (NULL!=pNode)
     {
         TiXmlNode 
*pParNode =  pNode->Parent();
         
if (NULL==pParNode)
         {
             
return false;
         }

         TiXmlElement
* pParentEle = pParNode->ToElement();
         
if (NULL!=pParentEle)
         {
             
if(pParentEle->RemoveChild(pNode))
                 m_pDoc
->SaveFile(m_strXmlFile);
             
else
                 
return false;
         }
     }
     
else
     {
         
return false;
     }

     
return false;
 }

 
bool CXmlParse::ModifyNode_Text(const string& strNodeName,const string& strText)
 {
     assert(NULL
!=m_pDoc);
     TiXmlElement 
*pRootEle = m_pDoc->RootElement();
     
if (NULL==pRootEle)
     {
         
return false;
     }

     TiXmlElement 
*pNode = NULL;

     GetNodePointerByName(pRootEle,strNodeName,pNode);

     
if (NULL!=pNode)
     {
         pNode
->Clear();  // 首先清除所有文本
         
// 然后插入文本,保存文件
         TiXmlText *pValue = new TiXmlText(strText);
         pNode
->LinkEndChild(pValue);
         m_pDoc
->SaveFile(m_strXmlFile);
         
return true;
     }
     
else
         
return false;
 }

 
bool CXmlParse::ModifyNode_Attribute(const string& strNodeName,
      
const map<string,string>& AttMap)
 {
     assert(NULL
!=m_pDoc);
     typedef std::pair 
<std::string,std::string> String_Pair;
     TiXmlElement 
*pRootEle = m_pDoc->RootElement();
     
if (NULL==pRootEle)
     {
         
return false;
     }

     TiXmlElement 
*pNode = NULL;
     GetNodePointerByName(pRootEle,strNodeName,pNode);

     
if (NULL!=pNode)
     {
         TiXmlAttribute
* pAttr = NULL; 
         std::
string strAttName = _T("");
         std::
string strAttValue = _T("");
         
for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
         {  
             strAttName 
= pAttr->Name();

            std::map
<std::string,std::string>::const_iterator iter;
             
for (iter=AttMap.begin();iter!=AttMap.end();iter++)
             {
                 
if (strAttName==iter->first)
                 {
                     pAttr
->SetValue(iter->second);
                 }
             }

         }  
         m_pDoc
->SaveFile(m_strXmlFile);
         
return true;
     }
     
else
     {
         
return false;
     }
 }


 
bool CXmlParse::ModifyNode_Attribute2(string strNodeName,string strAttValue,
     
const map<string,string> &AttMap)
 {
     assert(NULL
!=m_pDoc);
     typedef std::pair 
<std::string,std::string> String_Pair;
     TiXmlElement 
*pRootEle = m_pDoc->RootElement();
     
if (NULL==pRootEle)
     {
         
return false;
     }

     TiXmlElement 
*pNode = NULL;
     GetNodePointerByName_Attribute(pRootEle,strNodeName,strAttValue,pNode);

     
if (NULL!=pNode)
     {
         TiXmlAttribute
* pAttr = NULL; 
         std::
string strAttName = _T("");
         std::
string strAttValue = _T("");
         
for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
         {  
             strAttName 
= pAttr->Name();

             std::map
<std::string,std::string>::const_iterator iter;
             
for (iter=AttMap.begin();iter!=AttMap.end();iter++)
             {
                 
if (strAttName==iter->first)
                 {
                     pAttr
->SetValue(iter->second);
                 }
             }

         }  
         m_pDoc
->SaveFile(m_strXmlFile);
         
return true;
     }
     
else
     {
         
return false;
     }
 }

 
bool CXmlParse::GetAppInfo(MyAppInfo& Info)
 {
     map
<string,string> AttMap;
     
bool bSucc = QueryNode_Attribute(string(_T("Framework")),AttMap);
     
     std::map
<std::string,std::string>::iterator iter;
     
for (iter=AttMap.begin();iter!=AttMap.end();iter++)
     {
         
if (string(_T("AppName"))==iter->first)
         {
             Info.m_strAppName 
= iter->second;
         }

         
else if (string(_T("company"))==iter->first)
         {
             Info.m_strCompanyName 
= iter->second;
         }

         
else if (string(_T("url"))==iter->first)
         {
             Info.m_strUrl 
= iter->second;
         }
     }

     
return bSucc;
 }
复制代码

注意,上面的CXmlParse类在封装API接口操作的同时,结合一个用户自定义结构MyAppInfo来解析xml文件的内容。

   简单测试:

Xml文件的内容如下:

复制代码
<?xml version="1.0" encoding="GB2312" standalone="no" ?>
<BoostBind>
    
<Framework AppName="boost库测试程序 V1.0" company="BigHardware company" url="http:www.BigHardware.com" />
    
<UIDescription>
        
<AppMenuBar valid="1" caption="文件(F)">
            
<MenuItem valid="1" identity="file_new"  caption="新建" />
            
<MenuItem valid="1" identity="file_open"  caption="打开" />
        
</AppMenuBar>
           
<AppMenuBar valid="1" caption="编辑(E)">
            
<MenuItem valid="1" identity="edit_copy"  caption="拷贝" />
            
<MenuItem valid="1" identity="edit_paste"  caption="粘贴" />
        
</AppMenuBar>
    
</UIDescription>
</BoostBind>
复制代码

        现在我们要获取Framework节点的信息,将其填充到MyAppInfo类型的变量中,具体代码如下:

 CXmlParse SysSetting;
   SysSetting.OpenXml(
string(_T("F:\\MyTest\\MyTest\\src\\outdir\\debug\\SysConfig.xml")));
MyAppInfo Info;
SysSetting.GetAppInfo(Info);
原文地址:https://www.cnblogs.com/hnfxs/p/3117236.html