bogusbonafide - 2012-04-24

I want to generate an XML of the format,

    <Library>
    <ConfigParams>
       <parameter name= "name1"> abc </parameter>
       <parameter name= "name2"> def </parameter>
    </ConfigParams>
    <Books>
     <Book>
        <name>xyz</name>
        <title>jlkjlkj</title>
         <Parameters>
             <parameter name= "name3">name1 </parameter>
             <parameter name= "name4"> name4 </parameter>
         </Parameters>
      </Book>
       <Book>
           <name>xyzkjk</name>
        <title>jlj</title>
         <Parameters>
             <parameter name= "name8">name22 </parameter>
             <parameter name= "name7"> name44 </parameter>
         </Parameters>
        </Book>
    </Books>
    </Library>

For this I decided to use TinyXML:

I created following classes:

     class Param
     {
      public:
        string value;
string name;

Param();

Param( string value, string name)
{
  this->value=value;
  this->name=name;
}

      };
 

    class Parameters
    {
    public:
list<Param> param;

Parameters();
    Parameters(list<Param> param)
     {
     this->param = param;
     }

    };

    class Book
    {
    public:
string name;
string title;
Parameters parameters;

Book();
Book(string name, string title, Parameters parameters)
{
this->name = name;
this->title = title;
this->parameters = parameters;
}
    };

    class Books
    {
    public:
list<Book> books;

Books();
Books(list<Book> books)
{
this->books = books;
}
    };

    class Library
    {
    public:
Parameters configParams;
Books books;

Library();
Library(Parameters configparams, Books books)
{
this->configParams=configparams;
this->books=books;
}

void save(const char* Filename);
    };

    class ConfigParams
    {
    public:
Parameters configparams;

ConfigParams();
ConfigParams(Parameters configparams)
{
this->configparams = configparams;
}
    };

Now, when I want to generate an Xml document for this class hierarchy,

    void Library::save(const char* Filename)
    {

TiXmlDocument doc; 
TiXmlElement* msg;

TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); 
doc.LinkEndChild( decl );

TiXmlElement * root = new TiXmlElement("Library"); 
doc.LinkEndChild( root ); 

//block configparams
{
TiXmlElement * configparams= new TiXmlElement( "ConfigParams" ); 
        root->LinkEndChild( configparams );

        Param param1=Param("name1", "abc");
        Param param2=Param("name2" ,"def");

list<Param> params =list<Param>();
list <Param>::iterator Iter;

Iter=params.begin();
params.insert(Iter,param1);
params.insert(Iter,param2);
   
Parameters parameters=Parameters(params);

ConfigParams configparams=ConfigParams(parameters);

But, I dont know how to add these classes into XML form using TinyXml?