1

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)

roulibic
  • 325
  • 3
  • 9
  • What is the question? – ceving May 06 '15 at 08:44
  • 1
    The question is `How to create a XML file with the default format` / what should I do to solve my problem, I have some boxes, and I don't know how many box may be, and I want to make a xml file for them – roulibic May 06 '15 at 08:47
  • Ever tried a `for` loop? – ceving May 06 '15 at 12:02
  • 1
    yes, but a loop that make for example 3 box with all information, I can't say `If the box have weight then weight2 if not don't have this tag ` – roulibic May 07 '15 at 07:27
  • @chaedrae the update to your question are actually two new questions. 1. Use another `if` statement. 2. look into how to parse C strings. – Lydia Ralph May 07 '15 at 17:31
  • yes bus these are related questions, is it possible to write at the end of xml file? – roulibic May 08 '15 at 08:46

1 Answers1

1

The standard practice I have seen in most places is to have a schema file (an XSD file) which states the acceptable format.

<xs:element name="boxName" maxOccurs="unbounded">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Colour" type="xs:myStringType"  minOccurs="0"/>
                <xs:element name="From" type="xs:date"  minOccurs="0"/>
                <xs:element name="Weight" type="xs:int" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

The minOccurs means "this element is optional"

The maxOccurs="unbounded" means "as many as necessary"

You would then have checks in place to ensure that any XML conforms to this schema.

You could then easily use a for loop:

// pseudo-code
for each box,
    if colour variable exists, create and add colour element
    if weight variable exists, create and add weight element
    if from variable exists, create and add from element
Lydia Ralph
  • 1,455
  • 1
  • 17
  • 33
  • Thank you, I'm codding in C/C++, I didn't understand how and where to use this schema file, sorry if it's a bad the question, this is the first time that I'm creating xml file – roulibic May 06 '15 at 09:19
  • @chaedrae I've edited the question slightly to add C/C++ tags, hopefully it will get more attention – Lydia Ralph May 06 '15 at 09:53
  • thanks, should I create an xsd file before I write the program and then read it in the program? – roulibic May 06 '15 at 09:59
  • 1
    Yes, usually the XSD file would exist outside your code, and you can convert it into C++ objects automatically, e.g. see http://stackoverflow.com/questions/445905/xml-schema-to-c-classes – Lydia Ralph May 06 '15 at 10:06