TinyXML中部分函数的使用

void AddNewTest()
{
 //添加一个完整结点
 TiXmlElement *pEle = NULL;
 TiXmlNode *pNode = NULL;


 TiXmlDocument XMLDoc("AddInfo.xml");

 /*
  首先添加一个头部申明
 */
 TiXmlDeclaration *pDeclearation = new TiXmlDeclaration( "1.0","utf-8", "yes" ); //这部分内存不需要外部删除
 XMLDoc.LinkEndChild(pDeclearation); //将内存交给TinyXML释放

 /*
  添加根节点,使用InsertEndChild
 */
 TiXmlElement *pRootEle = new TiXmlElement("ROOT");
 TiXmlText *pText = new TiXmlText("");

 pNode = XMLDoc.LinkEndChild(pRootEle);
 pEle = pNode->ToElement();
 pNode = pEle->InsertEndChild(*pText); //该部分内存需要自己释放

 /*
  使用 InsertAfterChild 方法添加结点
 */
 TiXmlElement *pChildEle = new TiXmlElement("first child");
 pChildEle->SetAttribute("attr1",123);
 pChildEle->SetAttribute("attr2", "first");
 pNode = pRootEle->InsertAfterChild(pNode, *pChildEle);
 pEle = pNode->ToElement();
 pEle->InsertEndChild(*pText);

 /*
  使用 InsertEndChild 添加结点
 */
 DeleteSpecAttr(pChildEle, "attr2");
 pChildEle->SetAttribute("attr2", "second-add"); //添加需要修改的值
 pNode = pRootEle->InsertBeforeChild(pNode, *pChildEle);
 pEle = pNode->ToElement();
 pEle->InsertEndChild(*pText);

 /*
      设置结点的值 SetValue
 */
 DeleteSpecAttr(pChildEle, "attr2");
 pNode = pRootEle->InsertAfterChild(pNode, *pChildEle);
 pEle = pNode->ToElement();
 pText->SetValue("setvalue");  //设置结点的值
 pEle->InsertEndChild(*pText);


 if (pChildEle)
 {
  delete pChildEle;
  pChildEle = NULL;
 }

 if (pText)
 {
  delete pText;
  pText = NULL;
 }


 XMLDoc.Print();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值