Reputation: 26868
What library to use to write XML file in a C++ program?
I've found two classes posted in CodeProject
but want to check if there is more standard option than these. I'm only concerned with writing, and not parsing XML.
Upvotes: 7
Views: 11294
Reputation: 100638
I ran into the same problem and wound up rolling my own solution. It's implemented as a single header file that you can drop into your project: xml_writer.h
And it comes with a set of unit tests which also serve as documentation.
Upvotes: 0
Reputation: 595295
I have been using the open-source libxml2 library for years, works great for me.
Upvotes: 0
Reputation: 1237
For my purposes, PugiXML worked out really nicely
The reason why I thought it was so nice was because it was simply 3 files, a configuration header, a header, and the actual source.
But as you stated, you aren't interested in parsing, so why even bother using a special class to write out XML? While maybe your classes are too complex for this, I found the easy thing to do is use the std::ostream
and just write out standard compliant XML this way. For example, say I have a class that represents a Company
which is a collection of Employee
objects, just make a method in each the Company
and Employee
classes 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
's writeXML function can be declared virtual
so that you can have a specific output for say a CEO
, President
, Janitor
or whatever the subclasses should be.
Upvotes: 0
Reputation: 59269
I've been in a similar situation. I had a program that needed to generate JSON. We did it two ways. First we tried jsoncpp, but in the end I just generated the JSON directly via a std::ofstream
.
Afterward we ran the generated JSON through a validator to catch any syntax errors. There were a few but they were really easy to find and correct.
If I were to do it again I would definitely roll my own again. Somewhat unexpectedly, there was less code when using std::ofstream
. Plus we didn't have to use/learn a new API. It was easier to write and is easier to maintain.
Upvotes: -1
Reputation: 4038
You can use Xerces-C++, a library written by Apache foundation. This library permits read, write and manipulate XML files.
Link: http://xerces.apache.org/xerces-c/
Upvotes: 1
Reputation: 18984
Question: Are you ever going to update an XML file? Because while that sounds like it's just more writing, with XML it still requires a parser.
While xerces is large and bloated, it is fully standards compliant and it is DOM based. Should you ever have to cross platform or change language, there will always be a DOM based library for whatever language/platform you might move to so knowing how DOM based parsing/writing works is a benefit. If you are going to use XML, you may as well use it correctly.
Avoiding XML altogether is of course the best option. But short of that, I'd go with xerces.
Upvotes: 4