roulibic
roulibic

Reputation: 325

C: How to create a XML file with the default format

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)

Upvotes: 1

Views: 531

Answers (1)

Lydia Ralph
Lydia Ralph

Reputation: 1473

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

Upvotes: 1

Related Questions