boostdoctypeboost-propertytree

How to set target XML doctype using boost::property_tree::write_xml?


I would like to set the DOCTYPE of the generated XML document by boost::property_tree::write_xml:

#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main()
{
    using ptree = boost::property_tree::ptree;

    ptree pt;
    pt.put("ExampleKey", "ExampleValue");
    
    std::ofstream file("test.xml");
    boost::property_tree::write_xml(file, pt);

}

I tried with xml_writer_settings but there is very little (to be kind) useful documentation about it. So I am not even aware if it can help or if it purpose is totally different.

How to set the DOCTYPE in the generated XML by boost::property_tree::write_xml?


Solution

  • You can't. Boost Property Tree, unsurprisingly, is not an XML library. It's a property tree library.

    To write XML, consider using an XML library: What XML parser should I use in C++?

    Then again, there is potentially a hack using undocumented interface: removing encoding attribute from xml using boost

    That way you effectively bypass the document writing code and you can substitute it with your own hack.