0

我正在使用 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>

我不知道如何创建 day 标签的新元素。提前感谢您的帮助。

4

2 回答 2

1

我以前没有使用过 TinyXml,但是查看代码的结构,您需要在for循环中创建 day 元素并将其添加到 week 元素 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);
于 2010-11-11T23:30:38.217 回答
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;
}
于 2015-08-23T19:17:24.723 回答