找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1458|回复: 0

[分享] TinyXML使用技巧

[复制链接]

已领礼包: 145个

财富等级: 日进斗金

发表于 2014-9-19 10:39:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
现在越来越多的数据和配置采用了xml格式来存放和进行传输解析了。在c++方面,没有本地支持的库,所以需要我们自己去找一下。微软的msxml说实话,确实不咋地,尤其是com的类型变量名字一直指针,让众人看上去就比较反感。开源的tinyxml在这方便做的还不错。简单介绍下使用过程的一点小经验。
在这里发下牢骚,VC6.0以后的各个版本的VS环境对于C++的智能感知都是那么的SB,不管你怎么配置,怎么google都让你非常抓狂,就是不出来。算了不说了。

从网站上下载tinyxml,下载之后解压打开文件夹,里面有一些测试例子,tinyxml.sln支持vs2010了都,不管这些,怎么需要的是那个xml类库。
使用tinyxml我们只需要

tinyxml.cpp,

[url=]tinyxml.h[/url],
tinyxmlerror.cpp,

tinyxmlparser.cpp,

tinystr.cpp,

[url=]tinystr.h[/url]
6个文件即可。注意一旦少拷贝了其中tinyxmlerror.cpp, tinyxmlparser.cpp, 其中一个或者两个,就会报告link错误,呵呵
在目标源文件的头部,添加 #include"tinyxml.h"和#include "tinystr.h"

我们使用xml文件,无外乎这几种操作,
1. 遍历整个xml返回一棵树select
2. 查找特定节点的属性/文本值select
3.插入特定位置一个节点insert
4.更新特定节点的属性/文本值 update

5.创建xml文件 create
6.who knows

据一位网友的博文里面提到,基本应该涵盖数据库的所有操作,xml操作应该像操作数据库一样。我觉得甚是有道理啊。
那么我来列举下,我搜集以及测试成功的相关的操作代码吧,希望能节省一些大家学习的时间。

1.create xml操作

[cpp] view plaincopyprint?

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

// 定义一个TiXmlDocument类指针      TiXmlDocument *pDoc = new TiXmlDocument;      if (NULL==pDoc)      {          return false;      }      // 定义一个xml文件头部声明    TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(("1.0"),(""),(""));      if (NULL==pDeclaration)      {          return false;      }      pDoc->LinkEndChild(pDeclaration);      // 生成一个根节点:MyApp      TiXmlElement *pRootEle = new TiXmlElement(("MyApp"));      if (NULL==pRootEle)      {          return false;      }      pDoc->LinkEndChild(pRootEle);      // 生成子节点:Messages      TiXmlElement *pMsg = new TiXmlElement(("Messages"));      if (NULL==pMsg)      {          return false;      }      pRootEle->LinkEndChild(pMsg);      // 生成子节点:Welcome      TiXmlElement *pWelcome = new TiXmlElement(("Welcome"));      if (NULL==pWelcome)      {          return false;      }      pMsg->LinkEndChild(pWelcome);      // 设置Welcome节点的值      const char* strValue = ("Welcome to MyApp");      TiXmlText *pWelcomeValue = new TiXmlText(strValue);      pWelcome->LinkEndChild(pWelcomeValue);      // 生成子节点:Farewell      TiXmlElement *pFarewell = new TiXmlElement(("Farewell"));      if (NULL==pFarewell)      {          return false;      }      pMsg->LinkEndChild(pFarewell);      // 设置Farewell节点的值      strValue = ("Thank you for using MyApp");      TiXmlText *pFarewellValue = new TiXmlText(strValue);      pFarewell->LinkEndChild(pFarewellValue);      // 生成子节点:Windows      TiXmlElement *pWindows = new TiXmlElement(("Windows"));      if (NULL==pWindows)      {          return false;      }      pRootEle->LinkEndChild(pWindows);      // 生成子节点:Window      TiXmlElement *pWindow = new TiXmlElement(("Window"));      if (NULL==pWindow)      {          return false;      }      pWindows->LinkEndChild(pWindow);      // 设置节点Window的值      pWindow->SetAttribute(("name"),("MainFrame"));      pWindow->SetAttribute(("x"),("5"));      pWindow->SetAttribute(("y"),("15"));      pWindow->SetAttribute(("w"),("400"));      pWindow->SetAttribute(("h"),("250"));      // 生成子节点:Window      TiXmlElement *pConnection  = new TiXmlElement(("Connection"));      if (NULL==pConnection)      {          return false;      }      pRootEle->LinkEndChild(pConnection);      // 设置节点Connection的值      pConnection->SetAttribute(("ip"),("192.168.0.1"));      pConnection->SetAttribute(("timeout"),("123.456000"));      pDoc->SaveFile("1.xml");  
2.遍历打印xml文件 select操作

[cpp] view plaincopyprint?

  • //TiXmlDocument *pDoc = new TiXmlDocument();   
  •     if (NULL==pDoc)
  •     {
  •         return false;
  •     }
  •     pDoc->LoadFile("1.xml");
  •     pDoc->Print();

//TiXmlDocument *pDoc = new TiXmlDocument();      if (NULL==pDoc)      {          return false;      }      pDoc->LoadFile("1.xml");      pDoc->Print();  
3.获取单个节点值

[cpp] view plaincopyprint?

  • 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;
  • }

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;  }   
[cpp] view plaincopyprint?

  • 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)
  •    {
  •         strText = pNode->GetText();
  •         return true;
  •    }
  •    else
  •    {
  •         return 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)     {          strText = pNode->GetText();           return true;     }     else     {          return false;     }        }  
[cpp] view plaincopyprint?

  • 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;
  • }

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;  }  
4.删除节点操作

[cpp] view plaincopyprint?

  • 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;
  • }

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;  }  
5.修改节点操作

[cpp] view plaincopyprint?

  • bool ModifyNode_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)
  •     {
  •         pNode->Clear();  // 首先清除所有文本   
  •         // 然后插入文本,保存文件   
  •         TiXmlText *pValue = new TiXmlText(strText);
  •         pNode->LinkEndChild(pValue);
  •         pDoc->SaveFile(XmlFile);
  •         return true;
  •     }
  •     else
  •         return false;
  • }

bool ModifyNode_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)      {          pNode->Clear();  // 首先清除所有文本          // 然后插入文本,保存文件          TiXmlText *pValue = new TiXmlText(strText);          pNode->LinkEndChild(pValue);          pDoc->SaveFile(XmlFile);          return true;      }      else          return false;  }  
[cpp] view plaincopyprint?

  • bool ModifyNode_Attribute(std::string XmlFile,std::string strNodeName,
  •                  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>::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;
  •     }
  • }

bool ModifyNode_Attribute(std::string XmlFile,std::string strNodeName,                   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>::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;      }  }  
6增加节点操作

[cpp] view plaincopyprint?

  • bool AddNode_Text(std::string XmlFile,std::string strParNodeName,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,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;
  • }

bool AddNode_Text(std::string XmlFile,std::string strParNodeName,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,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;        }  
[cpp] view plaincopyprint?

  • bool AddNode_Attribute(std::string XmlFile,std::string strParNodeName,std::string strNodeName,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>::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;
  • }

bool AddNode_Attribute(std::string XmlFile,std::string strParNodeName,std::string strNodeName,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>::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;  }  

如果你想了解更多的操作以及细化,请参看参考文档了。


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-6-18 21:53 , Processed in 0.354524 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表