tinyxml 和 c++保存数据

发布于 2024-10-02 17:57:03 字数 2497 浏览 5 评论 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>

我无法弄清楚如何创建日期标签的新元素。预先感谢您的任何帮助。

I am using tinyxml to save data input by the user in a c++ console program. I pass a save function an array of structs that look like the following

struct day
{
      string name;
      string note;
};

I have seven of these, and pass all seven to the save function that looks like the following

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";
}

It writes this to the file

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

What i need is this

<?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>

I can't figure out how to create new elements of the day tag. Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-10-09 17:57:04

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

您当前的代码仅在最后将日元素添加到周元素一次 - 这反映在您的 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);

I haven't used TinyXml before but looking at the structure of the code, you need to create the day element inside your for loop and add it to the week element 7 times - once for each day.

Your current code only adds the day element to the week element once at the end - this is reflected in your xml output.

Taking part of your code - maybe something similar to this below. (This may not compile or be exactly correct but should provide the right idea).

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);
冰之心 2024-10-09 17:57:04
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文