0
我对TinyXml相当陌生,而且遇到了一些问题。我正在尝试学习如何创建和读取文件。
我可以从这个例子中读取数据用小xml保存和加载文件
<?xml version="1.0" standalone=no>
<!-- Our to do list data -->
<ToDo>
<Item priority="1"> Go to the <bold>Toy store!</bold></Item>
<Item priority="2"> Do bills</Item>
</ToDo>
提供的主页上,但是当我重新创建这个文件时,它无法加载
这里是我的代码来重新创建该文件:
TiXmlDocument document;
TiXmlElement * root = 0;
TiXmlDeclaration* declar = 0;
TiXmlComment * comment = 0;
// Create declaration
declar = new TiXmlDeclaration("1.0", "", "no");
// Link it to doc
document.LinkEndChild(declar);
// Create Comment
comment = new TiXmlComment();
comment->SetValue("Our to do list data");
// Link it to doc
document.LinkEndChild(comment);
// Create root and Link it
root = new TiXmlElement("ToDo");
document.LinkEndChild(root);
// Create item 1 element
TiXmlElement* item1 = new TiXmlElement("item");
// Set Its attribute priority 1
item1->SetAttribute("priority", "1");
// Link text element
TiXmlText* item1Text = new TiXmlText("Go To The");
item1->LinkEndChild(item1Text);
// Create item1 Bold element
TiXmlElement* item1Bold = new TiXmlElement("Bold");
// Link Text element to bold element
TiXmlText* boldText = new TiXmlText("Toy Store");
item1Bold->LinkEndChild(boldText);
// Link bold Element to Item element
item1->LinkEndChild(item1Bold);
// Link item element to root node
root->LinkEndChild(item1);
// Create item 2 element
TiXmlElement* item2 = new TiXmlElement("item");
// Set its attribute priority 2
item2->SetAttribute("priority", "2");
// And Link Text Item
TiXmlText* item2Text = new TiXmlText("Do Bills");
item2->LinkEndChild(item2Text);
// Link another item element
root->LinkEndChild(item2);
// Save
document.SaveFile("TestFile.xml");
你能告诉我什么我失踪或做错了吗?
为什么你不存储指向你'新'的东西?它使你的代码看起来非常复杂,并且可能是你的错误的原因。另外,我认为你根本不需要使用'new' ... – 2015-02-24 01:00:33
你说这个文件加载失败,但是你显示的代码是创建该文件的代码,而不是加载它。 – 2015-02-24 01:02:41
没有'new':http://coliru.stacked-crooked.com/a/6b0736462e8e14bd – 2015-02-24 01:06:27