我对TinyXml很新,我遇到了一些问题。我正在尝试学习如何创建和阅读文件。
我可以从这个例子中读取数据
<?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");
你能告诉我我错过了什么或做错了吗?
答案 0 :(得分:0)
好的,这里是问题的解决方案我在将其他元素链接到root之前将根节点链接到文档,因此在保存方法调用解决问题之前将其移动到最后谢谢!!
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");`