2010-11-11 78 views
0

我正在使用tinyxml來保存用戶在C++控制檯程序中輸入的數據。我通過保存功能結構的數組,看起來像下面tinyxml和C++保存數據

struct day 
{ 
     string name; 
     string note; 
}; 

我有這七個,並通過所有七保存功能,看起來像下面

void saveData(day dayArr[]) 
{ 
    TiXmlDeclaration* declaration = new TiXmlDeclaration("1.0", "UTF-8", "no");//Create DTD 
    TiXmlDocument* doc = new TiXmlDocument; 
    doc->LinkEndChild(declaration); 

    TiXmlElement* week = new TiXmlElement("week"); 
    TiXmlElement* day = new TiXmlElement("day"); 
    TiXmlElement* name = new TiXmlElement("name"); 
    TiXmlElement* note = new TiXmlElement("note"); 
    TiXmlElement* tl = new TiXmlElement("tl"); 
    TiXmlElement* ti = new TiXmlElement("ti"); 
    TiXmlText* dayName = new TiXmlText(""); 
    TiXmlText* dayNote = new TiXmlText(""); 

    for(int i=0; i<7; i++) 
    { 
     dayName = new TiXmlText(dayArr[i].name.c_str()); 
     dayNote = new TiXmlText(dayArr[i].note.c_str()); 
     name->LinkEndChild(dayName); 
     note->LinkEndChild(dayNote); 
     day->LinkEndChild(name); 
     day->LinkEndChild(note); 
    } 

    week->LinkEndChild(day); 
    doc->LinkEndChild(week); 

    doc->SaveFile("test.xml"); 
    cout << "SAVED"; 
} 

它寫出這個該文件

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<week> 
    <day> 
     <name>SundayMondayTuesdayWednesdayThursdayFridaySaturday 
     </name> 
     <note> 
     </note> 
    </day> 
</week> 

我需要的是這個

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<week> 
    <day> 
     <name>Sunday</name> 
     <note>  </note> 
    </day> 
<day> 
     <name>Monday</name> 
     <note> 
     </note> 
    </day> 
<day> 
     <name>Tuesday</name> 
     <note>  </note> 
    </day> 
<day> 
     <name>Wednesday</name> 
     <note>  </note> 
    </day> 
<day> 
     <name>Thursday</name> 
     <note>  </note> 
    </day> 
<day> 
     <name>Friday</name> 
     <note>  </note> 
    </day> 
<day> 
     <name>Saturday</name> 
     <note>  </note> 
    </day> 
</week> 

我想不出如何創建天標記的新元素。預先感謝您的幫助。

回答

1

我之前沒有使用過TinyXml,但是在查看代碼的結構時,您需要在for循環內創建day元素並將其添加到7次元素中 - 每天一次。

您當前的代碼只在最後一次將day元素添加到week元素 - 這反映在您的xml輸出中。

代碼的一部分 - 可能類似於下面的內容。 (這可能不會編譯或者完全正確,但應該提供正確的想法)。

TiXmlElement* week = new TiXmlElement("week"); 
TiXmlElement* name = new TiXmlElement("name"); 
TiXmlElement* note = new TiXmlElement("note"); 
TiXmlElement* tl = new TiXmlElement("tl"); 
TiXmlElement* ti = new TiXmlElement("ti"); 
TiXmlText* dayName = new TiXmlText(""); 
TiXmlText* dayNote = new TiXmlText(""); 

for(int i=0; i<7; i++) 
{ 
    TiXmlElement* day = new TiXmlElement("day"); 
    dayName = new TiXmlText(dayArr[i].name.c_str()); 
    dayNote = new TiXmlText(dayArr[i].note.c_str()); 
    name->LinkEndChild(dayName); 
    note->LinkEndChild(dayNote); 
    day->LinkEndChild(name); 
    day->LinkEndChild(note); 
    week->LinkEndChild(day); 
} 

doc->LinkEndChild(week); 
+0

我知道了。我正在學習如何使用tinyxml,並且應該看到(真正的時刻)非常感謝您的幫助。它不讓我發表另一個答案。但是,謝謝! – Bear 2010-11-11 23:33:47

0
void saveData(std::vector<day*> vecDay) 
{ 
    TiXmlDeclaration* declaration = new TiXmlDeclaration("1.0", "UTF-8", "no");//Create DTD 
    TiXmlDocument* doc = new TiXmlDocument; 
    doc->LinkEndChild(declaration); 

    TiXmlElement* week = new TiXmlElement("week"); 

    for(std::vector<day*>::iterator it = vecDay.begin(); it != vecDay.end(); it++) 
    { 
     TiXmlElement* day_ = new TiXmlElement("day"); 
     TiXmlElement* name = new TiXmlElement("name"); 
     TiXmlElement* note = new TiXmlElement("note"); 
     TiXmlElement* tl = new TiXmlElement("tl"); 
     TiXmlElement* ti = new TiXmlElement("ti"); 
     TiXmlText* dayName = new TiXmlText(""); 
     TiXmlText* dayNote = new TiXmlText(""); 
     dayName = new TiXmlText((*it)->name.c_str()); 
     dayNote = new TiXmlText((*it)->note.c_str()); 
     name->LinkEndChild(dayName); 
     note->LinkEndChild(dayNote); 
     day_->LinkEndChild(name); 
     day_->LinkEndChild(note); 
     week->LinkEndChild(day_); 
    } 

    doc->LinkEndChild(week); 

    doc->SaveFile("test2.xml"); 
    cout << "SAVED" << endl; 
}