C ++ object for XML for communication

I am looking for an easy way to convert a C ++ object into a string representation of XML, so that I could communicate with the server.

For example, let's 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: (Suppose I created one instance of 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> 
+6
source share
3 answers

What you are describing is called XML data binding. There are a number of products that will generate C ++ and XSD or DTD code, see http://www.xmldatabinding.org/ for a list, and http://www.rpbourret.com/xml/XMLDataBinding.htm for more information. .

Also consider this example XML data binding for C ++ , it shows an example source schema and generated code.

If your circuits are fairly simple, and you have the option of setting them up for a generator, there may be some open source projects that will do the job. If you bind to the XML standard, you quickly come across the limitations of most generators. The Liquid XML Generator handles almost all XSD standards, but you have to pay for it.

+3
source

There is no universal solution to this problem in C ++, however, there are many adhoc implementations.

There are some great links and practical recommendations on this question: How to implement serialization in C ++

+3
source

So, there is no standard way, because just put it, because there is no way to serialize pointers and the like. It will always be application specific.

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

As for the xml parsers, have you tried this ? It is extremely simple, effective and easy to learn. I basically did everything with this. You can even request a commercial license.

+2
source

Source: https://habr.com/ru/post/915044/


All Articles