Search code examples
xmllistc++-clixmlwriter

C++ list in combination with xmlwriter


My problem is to output a list in C++ using XmlWriter. I need a list wich can be included in my XML file. My planned code - A class should be implemented to generate the list elements, but I don't know why:

writer->WriteStartElement( "Parameters" );

    writer->WriteStartElement( "ParamterList" );
               // A list including approximately 100 entries
               writer->WriteAttributeString( "ID", "001" );
               writer->WriteAttributeString( "Name", "Dummy1" );
               writer->WriteAttributeString( "BitOffset", "0" );
    writer->WriteEndElement();

writer->WriteEndElement();

How is it possible to insert a list into my application. My output should look like this:

<root Name="database" Purpose="test" Project="test">
<Description Version="1.1B" Author="name">test</Description>
<ContainerList>
    <Container Name="Dummy1" BitOffset="0" />
</ContainerList>
<ParameterList>
    <Paramter ID="001" Name="Dummy1" BitOffset="0" />
    <Paramter ID="002" Name="Dummy2" BitOffset="1" />
    <Paramter ID="003" Name="Dummy3" BitOffset="0" />
    <Paramter ID="004" Name="Dummy4" BitOffset="0" />
     ......
</ParameterList>

Due to the code shown in answer 1, I now know how to use a list with XmlWriter, but at the moment I don't really know how to connect and write a class for this code?


Solution

  • Each Paramter (did you spell that correctly?) element is an XML element. Therefore, it needs a WriteStartElement call and a WriteEndElement. Within those call, you add your IDs and other attributes. So your list would be something like this:

    for (auto listElement :list) //Using C++0x syntax
    {
      writer->WriteStartElement( "Paramter" );
      writer->WriteAttributeString( "ID", listElement.id() );
      writer->WriteAttributeString( "Name", listElement.name() );
      writer->WriteAttributeString( "BitOffset", listElement.bitOffset() );
      writer->WriteEndElement();
    }