2015-02-24 109 views
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"); 

你能告訴我什麼我失蹤或做錯了嗎?

+0

爲什麼你不存儲指向你'新'的東西?它使你的代碼看起來非常複雜,並且可能是你的錯誤的原因。另外,我認爲你根本不需要使用'new' ... – 2015-02-24 01:00:33

+1

你說這個文件加載失敗,但是你顯示的代碼是創建該文件的代碼,而不是加載它。 – 2015-02-24 01:02:41

+0

沒有'new':http://coliru.stacked-crooked.com/a/6b0736462e8e14bd – 2015-02-24 01:06:27

回答

0

好的,這裏是解決問題的方法我已經將根節點鏈接到文檔,然後將其他元素鏈接到根,因此在保存方法調用之前將其移動到最後,解決了問題,謝謝!

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");` 
我聯繫根節點鏈接等元素根節點之前記錄的問題