I'm beginner in XML and I have this information:
box1 -> name, colour, from
box2 -> name, weight
box3 -> name, colour, from, weight
and I want to make one XML file like this:
<boxName>name1
<boxColour>colour1</boxColour>
<boxFrom>from1</boxFrom>
</boxName>
<boxName>name2
<boxColour>colour2</boxColour>
<boxWeight>weight2</boxWeight>
</boxName>
<boxName>name3
<boxColour>colour3</boxColour>
<boxFrom>from3</boxFrom>
<boxWeight>weight3</boxWeight>
</boxName>
I created my XML using TinyXml in this form:
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "utf-8", "");
doc.LinkEndChild( decl );
TiXmlElement* element = new TiXmlElement("boxName");
doc.LinkEndChild(element);
TiXmlText* text = new TiXmlText("name1");
element->LinkEndChild(text);
TiXmlElement* element2 = new TiXmlElement("boxColour");
TiXmlElement* element3 = new TiXmlElement("boxFrom");
TiXmlText* text2 = new TiXmlText("colour1");
TiXmlText* text3 = new TiXmlText(from1);
element->LinkEndChild(element2);
element->LinkEndChild(element3);
element2->LinkEndChild(text2);
element3->LinkEndChild(text3);
doc.SaveFile( "XML.xml" );
but the problem is that number of boxes is unknown and each box may have 1,2,3 or more child, but the format for each box and it's information is the same (as the above)
please help me to make the XML file
I am coding in C / API
Thanks
Update:
I can use a for loop just in this form:
for(int i=0; i<3; i++)
{
TiXmlElement* element2 = new TiXmlElement("element");
TiXmlText* text2 = new TiXmlText("text");
element->LinkEndChild(element2);
element2->LinkEndChild(text2);
}
1: I can't say If one box have weight then use <boxWeight> tag and add <boxWeight>weight2</boxWeight> if not don't have <boxWeight></boxWeight> tag
2: I have the boxes Information's in a buffer in this form:
box1 name:name1 coloure: coloure1 from: from1 I don't know how to split the information (C/API)