尝试将元素添加到现有节点时程序崩溃

时间:2012-05-06 19:26:10

标签: c++ elements tinyxml

我正在尝试将Element添加到已存在的Node / Parent中,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
   <BitComet Version="1.0">
      <MyShares>
         <bt title="Sound of Music" save_path="C:\Downloads|Sound of Music" task_finished="true" category="software" size="7126369" hash="31e0ded5b561ed698c151e72d5f20d9b75f03b12"/>
      </MyShares>
   </BitComet>

-

TiXmlDocument xml(pf);
if(!xml.LoadFile()) return;

TiXmlElement* root = xml.FirstChildElement("BitComet");

TiXmlElement* parent = xml.FirstChildElement("MyShares")

TiXmlElement* elem = new TiXmlElement( "bt" );
elem->SetAttribute("title", "TiTlE");
elem->SetAttribute("save_path", "C:\\Downloads|Test");
elem->SetAttribute("task_finished", "true");
elem->SetAttribute("category", "software");
elem->SetAttribute("size", 7126369);
elem->SetAttribute("hash", "hash_here");
xml.LinkEndChild( elem );

xml.LinkEndChild( parent );

xml.LinkEndChild( root );

xml.SaveFile(pf);

我不确定发生了什么,但在执行该部分代码时崩溃了。

1 个答案:

答案 0 :(得分:1)

我认为您访问<MyShares>标记的电话应为

TiXmlElement* parent = root -> FirstChildElement("MyShares");

此外,您想要的唯一LinkEndChild电话如下:

parent->LinkEndChild( elem );