boostorg / property_tree

Boost.org property_tree module
http://boost.org/libs/property_tree
55 stars 92 forks source link

Ability to suppress xml version/encoding processing instruction #46

Closed leppardb closed 10 months ago

leppardb commented 4 years ago

My use case is to inject xml snippets into comment blocks as templates for users to modify and un-comment as desired. Because the comment marker tag only accepts a comment string (subnodes not supported), I generate the xml snippets separately and write them to a stringstream then inject the string into the xml comment tag. Each time a snippet xml is generated the \<?xml version="1.0" encoding="utf-8"?> processing instruction is automatically inserted.

I want to be able to suppress emitting this instruction. I suggest that if the encoding attribute in xml_writer_settings is explicitly set to an "empty" string then automatic insertion of the version/encoding instruction be skipped.

TomFD commented 2 years ago

looking at the implementation, you can skip the instruction and only use the internal function xml_write_element. Here is a full example:

#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

int main() {
typedef typename boost::property_tree::ptree::key_type Str;

    boost::property_tree::ptree tree;
    tree.put("test", "somevalue");

    boost::property_tree::xml_parser::write_xml_element(
        std::cout, Str(),tree, -1,boost::property_tree::xml_writer_settings<std::string>(' ', 4));
}

to get the output

<test>somevalue</test>