What library to use for * writing * an XML file in a C ++ program?

What library can I use to write an XML file in a C ++ program?

I found two classes posted in CodeProject

but you want to check if there is a more standard option than these. I am only writing and not parsing XML.

+7
source share
7 answers

I tried different libraries and finally solved TinyXml . It is compact, fast, free (zlib-license) and very easy to use.

+4
source

Question: Are you going to update the XML file? Since while it sounds like it is more written, an XML parser is still required with XML.

While xerces is large and bloated, it is fully standards compliant and based on the DOM. If you ever have to switch to a platform or change a language, there will always be a DOM-based database for any language / platform you can switch to, so knowing how the DOM based on parsing / writing works is advantage. If you intend to use XML, you can use it correctly.

Of course, the best option is an XML exception. But other than that, I would go with the Xers.

+3
source

You can use Xerces-C ++, a library written by the Apache base. This library allows you to read, write and process XML files.

Link: http://xerces.apache.org/xerces-c/

+1
source

For my purposes, PugiXML did a great job

http://pugixml.org/

The reason I thought it was so nice was because it was just 3 files, the configuration header, the header and the actual source.

But, as you stated, you are not interested in parsing, so why even use a special class to write out XML? Although your classes may be too complicated for this, I found the easy task of using std::ostream and just std::ostream out standard compatible XML this way. For example, let's say I have a class representing Company , which is a collection of Employee objects, just create a method in each Company and Employee class that looks something like the following psuedocode

 Company::writeXML(std::ostream& out){ out << "<company>" << std::endl; BOOST_FOREACH(Employee e, employees){ e.writeXML(out); } out << "</company>" << std::endl; } 

and to make it even easier, your Employee writeXML function can be declared virtual so that you can have a specific output, for example, CEO , President , Janitor or whatever should be subclassed.

0
source

I have been using the open-source library libxml2 for years, great for me.

0
source

I ran into the same problem and hurt folding my own solution . It is implemented as one header file that you can add to your project: xml_writer.h

And it comes with a suite of unit tests that also serve as documentation.

0
source

Create your own

I was in a similar situation. I had a program that was supposed to generate JSON. We did this in two ways. At first we tried jsoncpp , but in the end I just generated JSON directly via std::ofstream .

After that, we launched the generated JSON through the validator to catch any syntax errors. There were several of them, but they were very easy to find and fix.

If I did it again, I would certainly throw my own again. Somewhat unexpectedly, when using std::ofstream there was less code. In addition, we did not have to use / learn the new API. It was easier to write and easier to maintain.

-one
source

All Articles