0

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?

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
PragJh
  • 41
  • 1
  • 4

1 Answers1

0

TinyXML is a DOM based XML parser/toolkit. In order to save it the XML to a file on disk, you will need to create a DOM i.e. an in-memory representation of the XML. I suggest you take a look at the TinyXML tutorial here, specifically at the example titled "Writing a document to a file".

You can add a virtual member save function to all classes that need to be serialized and pass them a reference to an ofstream. Now, you will need to have someone open a file stream and pass it to the Library class object, which will write to the stream its member information and then for each non-trivial member (i.e. of type say 'Param') will call the save function on the member. So, what you end up with is a chained set of calls to save where each building block is responsible for outputting their own information.

You can look at the way operator<< is implemented for user classes and get a start!

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • I saw that, i want to ask how to incorporate the classes as XML elements.Writing to the doc is what i know. how do i add configparams object into the XML element? – PragJh Apr 24 '12 at 09:37
  • When i run my program i get likner errors i dont know why I have included tinyxml.h in the include folder of VC. Still, its givng me the linker errors. Can you help ? – PragJh Apr 25 '12 at 05:35
  • Are you including the header files properly? Is there an implementation file (something like `tinyxml.cpp`) that you have not included in your project?You will need to post the error log for better responses though! – dirkgently Apr 26 '12 at 06:17