0

ive been working one some stuff for university

were to save some stuff and i chose tinyxml to do so

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* cm = new TiXmlElement("CocktailMachine");
doc.LinkEndChild(cm);

TiXmlElement* disp = new TiXmlElement("dispensers");
cm->LinkEndChild(disp);
TiXmlElement* cock = new TiXmlElement("cocktails");
cm->LinkEndChild(cock);
TiXmlElement* ingr = new TiXmlElement("ingredients");
cm->LinkEndChild(ingr);






for (size_t i = 0; i < dispensers.size(); i++)
{
    stringstream ss;
    ss << i;
    string s = ss.str();
    TiXmlElement* x_disp = new TiXmlElement(s.c_str());
    disp->LinkEndChild(x_disp);
    x_disp->SetAttribute("number", dispensers.at(i)->get_number());
    if (dispensers.at(i)->get_ingredient() == NULL) {

        x_disp->SetAttribute("ingredient", "NULL");

    }
    else {


        x_disp->SetAttribute("ingredient", dispensers.at(i)->get_ingredient()->get_name().c_str());

    }

}


if (cocktails.size() != 0)
    for (size_t i = 0; i < cocktails.size(); i++)
    {


        stringstream ss;
        ss << i;
        string s = ss.str();
        TiXmlElement* ct = new TiXmlElement(s.c_str());
        cock->LinkEndChild(ct);
        ct->SetAttribute("name", cocktails.at(i)->get_name().c_str());

        for (size_t j = 0; j < cocktails.at(i)->get_ingredients().size(); j++)
        {

            stringstream ss;
            ss << j;
            string s = ss.str();
            TiXmlElement* ct_s = new TiXmlElement(s.c_str());
            ct->LinkEndChild(ct_s);

            ct_s->SetAttribute("ingredient", cocktails.at(i)->get_ingredients().at(j)->get_name().c_str());
        }

    }



if (ingredients.size() != 0)
    for (size_t i = 0; i < ingredients.size(); i++)
    {
        stringstream ss;
        ss << i;
        string s = ss.str();
        TiXmlElement* x_ingr = new TiXmlElement(s.c_str());
        ingr->LinkEndChild(x_ingr);
        x_ingr->SetAttribute("name", ingredients.at(i)->get_name().c_str());

    }


doc.SaveFile("CM.xml");
doc.Clear();

this saves some stuff into the CM.xml

<?xml version="1.0" ?>
<CocktailMachine>
    <dispensers>
        <0 number="1" ingredient="NULL" />
        <1 number="2" ingredient="NULL" />
        <2 number="3" ingredient="NULL" />
        <3 number="4" ingredient="NULL" />
        <4 number="5" ingredient="NULL" />
        <5 number="6" ingredient="NULL" />
    </dispensers>
    <cocktails>
        <0 name="Tequila Sunrise">
            <0 ingredient="Tequila" />
            <1 ingredient="Orange juice" />
            <2 ingredient="Grenadine" />
        </0>
        <1 name="tq2">
            <0 ingredient="Tequila" />
            <1 ingredient="Orange juice" />
            <2 ingredient="Grenadine" />
        </1>
    </cocktails>
    <ingredients />
</CocktailMachine>
(not code i know but this was the only way to post the xml)

when i try to load it with this

TiXmlDocument* doc = new TiXmlDocument("example1.xml");
if (!doc->LoadFile())
    cout << "false";

it always returns false

trying to load the "helloworld.xml" is working fine

<?xml version="1.0" ?>
<Hello>World</Hello>

so where is the problem with my xml ?

thanks in advance Jonny0815

Jonny0815
  • 21
  • 2

2 Answers2

2

Here's the problem: <0 number="1" ingredient="NULL" />

You can't have a number as the tag name.

Daniel Enache
  • 391
  • 3
  • 5
1

thanks so much solved my problem guys :D

" 0 is not a valid XML name "

have a good weekend

OT: new here ... can i mark questions as "answered" or "done" or open a close request so everyhting stays ordered ?

Jonny0815
  • 21
  • 2