c++xmltagstinyxml

tinyxml and c++ to save data


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.


Solution

  • 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);