Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Object to XML for communication

I'm looking for a simple-way to transform in C++ an object into XML string representation, so this way I could communicate with a server.

For instance, let us say I have an object:

class A{
    string data1;
    string data2;
    string dataN;
    list<B> bList;
}

class B{
    string moreData;
}

I would like the following XML-representation: (Assume that I have created one instance A and it has two instances of B)

<A>
    <data1>content</data1>
    <data2>content</data2>
    <dataN>content</dataN>
    <B>
        <moreData>content</moreData>
    </B>    
    <B>
        <moreData>content</moreData>
    </B>
</A>
like image 620
Santi Agüero Avatar asked May 06 '12 22:05

Santi Agüero


3 Answers

What you describe is referred to as XML Data Binding. There are a number of products that will generate the C++ code from and XSD or DTD, have a look at http://www.xmldatabinding.org/ for a list, and http://www.rpbourret.com/xml/XMLDataBinding.htm for more information.

Also have a look at this XML Data Binding example for C++, it shows the example source schema and generated code.

If your schemas are pretty basic and you have the ability to tune them to the generator then there are probably some open source projects that will do the job. If you are binding to an XML Standard then you quickly run up against the limitations of most generators. The Liquid XML generator copes with pretty much all XSD standards, but you have to pay for it.

like image 97
Sprotty Avatar answered Sep 28 '22 06:09

Sprotty


There is no universal solution for this problem in C++, however many adhoc implementations exist.

This question has some remarkable links and how-tos: How to implement serialization in C++

like image 21
Sergey K. Avatar answered Sep 28 '22 08:09

Sergey K.


So, there is no standard way because, simply put, because there is no way of serializing pointers and things like that. It will always be application specific.

However, you can create your own class and serialize as you want.

As for xml parsers, have you tried this one? It is extremely simple, efficient, and easy to learn. I've done basically all with it. You can even ask for a commercial license to it.

like image 29
Jorge Leitao Avatar answered Sep 28 '22 06:09

Jorge Leitao