开发者

C++ list in combination with xmlwriter

开发者 https://www.devze.com 2023-03-24 11:48 出处:网络
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 kn

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>
<Parameter开发者_JAVA百科List>
    <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?


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号