天天看点

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释放

 TiXmlElement *pRootEle = new TiXmlElement("ROOT");

 TiXmlText *pText = new TiXmlText("");

 pNode = XMLDoc.LinkEndChild(pRootEle);

 pEle = pNode->ToElement();

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

 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);

 DeleteSpecAttr(pChildEle, "attr2");

 pChildEle->SetAttribute("attr2", "second-add"); //添加需要修改的值

 pNode = pRootEle->InsertBeforeChild(pNode, *pChildEle);

 pEle = pNode->ToElement();

 pEle->InsertEndChild(*pText);

 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();

}

继续阅读