c++ - tinyxml和c++保存数据

标签 c++ xml tags tinyxml

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

我不知道如何创建日标签的新元素。在此先感谢您的帮助。

最佳答案

我之前没有使用过 TinyXml,但查看代码结构,您需要在 for 循环中创建 day 元素并将其添加到 week 元素 7 次 - 每次一次一天。

您当前的代码仅在末尾将 day 元素添加到 week 元素一次 - 这反射(reflect)在您的 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);

关于c++ - tinyxml和c++保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4160214/

相关文章:

c++ - 制作模型加载器 : What to do after reading the vertices and texture?

c++ - mongocxx : Inserting a Datetime

java - JAXB 属性绑定(bind) - 为什么需要它

.net - 我在哪里可以找到 taglib-sharp 支持的所有音频文件类型的列表?

php - Instagram 最近的标签

c++ - 在非 Qt 应用程序 (C++) 中使用 Qt 的信号

c++ - 多线程效率

java - 单击按钮时无法打开 fragment

c# - 如何将 XElement 转换为 XDocument

Scaladoc (2.11.6) 在 throws 标记为 "unable to find any member to link"时失败