To begin with, I think you should have a common schema file that applies all of the XML files. Then you may get better results from the XSD tool. But you should not overestimate its capabilities as it completely ignores the content-inner text-attribute values etc and targets tag/attribute names to generate the corresponding code.
If you are looking for more customised approach look for text generation methods. Ruby erb is one of the powerful ones that I know. For instance
Generator.rb
require 'erb'
require 'rexml/document'
include REXML
def subClsHdr (superName,clsNode)
fh = File.open("header.erb", "r" )
eruby_script_header = fh.read
fc = File.open("source.erb", "r" )
eruby_script_cpp = fc.read
name = clsNode.attributes['Name']
fhw = File.open("#{name}.h","w")
fcw = File.open("#{name}.cpp","w")
fields = []
clsNode.elements.each('Field'){|f|
fields.push( [f.attributes['Type'], f.attributes['Name']]) }
clsNode.elements.each('Class'){|f| subClsHdr(name,f)}
erb = ERB.new(eruby_script_header)
code = erb.result( binding )
fhw.print code
fhw.close
erb = ERB.new(eruby_script_cpp)
code = erb.result( binding )
fcw.print code
fcw.close
end
file = File.new("test.xml")
doc = Document.new(file)
doc.elements.each('Class'){|t| subClsHdr("",t)}
header.erb
#ifndef <%= name.upcase %>_H_
#define <%= name.upcase %>_H_<%if superName.length > 0 %>
#include "<%= superName %>.h"<%end%>
#ifndef COMMONS_H_
#include "Commons.h"
#endif
class <%= name %><%if superName.length > 0 %> : public <%= superName %><% end %>
{
public:
<%= name %>();
virtual ~<%= name %>();
<%fields.each{|l|%> const <%=l[0]%> get<%=l[1].capitalize%>();
void set<%=l[1].capitalize%>(const <%=l[0]%>& param);<%}%>
private:
<%fields.each{|l|%> <%=l[0]%> <%=l[1]%>;<%}%>};
#endif //<%= name.upcase %>_H_
And input
<Class Name="Person">
<Field Name="Name" Type="std::string"></Field>
<Field Name="Status" Type="std::string"></Field>
<Field Name="Age" Type="int"></Field>
<Class Name="Worker">
<Field Name="ssid" Type="int"></Field>
<Class Name="Banker">
<Field Name="Position" Type="std::string"></Field>
</Class>
</Class>
</Class>
The output "Person.h":
#ifndef PERSON_H_
#define PERSON_H_
#ifndef COMMONS_H_
#include "Commons.h"
#endif
class Person
{
public:
Person();
virtual ~Person();
const std::string getName();
void setName(const std::string& param);
const std::string getStatus();
void setStatus(const std::string& param);
const int getAge();
void setAge(const int& param);
private:
std::string Name;
std::string Status;
int Age;
};
#endif //PERSON_H_
Output "Worker.h":
#ifndef WORKER_H_
#define WORKER_H_
#include "Person.h"
#ifndef COMMONS_H_
#include "Commons.h"
#endif
class Worker : public Person
{
public:
Worker();
virtual ~Worker();
const int getSsid();
void setSsid(const int& param);
private:
int ssid;
};
#endif //WORKER_H_
And so on with corresponding "cpp" files etc. You can of course go for Linq to Xml or some other way whichever fits your taste.
Hope it helps.