在《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修饰符。

最新文章

  1. 分布式系统理论进阶 - Paxos
  2. http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html
  3. [BS-04] 在iOS中对系统定义的类的readonly属性可通过KVC进行赋值
  4. C# 如何获取某个类型或类型实例对象的大小
  5. win7+ubuntu双系统安装攻略
  6. jQuery常用技巧大放送
  7. 第二部分----CSS的基础语法
  8. T-SQL基础(五)之增删改
  9. Grafana的基本使用
  10. 马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解
  11. CentOS-5的yum源无法使用问题
  12. MVC把表格导出到Excel
  13. eclipse中点不出来提示
  14. 内存对齐与ANSI C中struct型数据的内存布局
  15. 设计模式-单例模式下对多例的思考(案例:Server服务器)
  16. django_orm 基本操作
  17. 【Android】完善Android学习(三:API 3.0)
  18. Redis五大数据类型及操作
  19. Mac安装并破解StarUML
  20. jQuery九宫格图片拉伸变大代码

热门文章

  1. 嘟!数字三角形 W WW WWW集合!
  2. Java获取昨天的时间
  3. bootstrap首页制作
  4. yii 标签用法(模板)
  5. 不能执行已释放script的代码
  6. 关于几种常用的Adapter使用区别
  7. jQuery显示与隐藏返回顶层的箭头
  8. python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中
  9. PYTHON WEATHER
  10. XML PULL模型