omit xml declaration when saving xml with boost
Asked Answered
F

3

7

Is it possible, via the xml_writer_settings used as third parameter in the write_xml call, to omit the xml declaration when the function saves the xml? I mean, I would like not to have the initial "xml version="blah" encoding="blah blah" part. I'm searching the internet but I still haven't found an answer. How to do it?

Favian answered 17/4, 2013 at 10:57 Comment(1)
You can just trim the first string containing <?xml...> after write_xml()Lougheed
H
1

No, it's not possible. look here for members of xml_writer_settings

And too, write_xml calls write_xml_internal that is (in boost 1.52)

template<class Ptree>
void write_xml_internal(
std::basic_ostream<typename Ptree::key_type::value_type> &stream, 
const Ptree &pt,
const std::string &filename,
const xml_writer_settings<typename Ptree::key_type::value_type> & settings)
{
    typedef typename Ptree::key_type::value_type Ch;
    typedef typename std::basic_string<Ch> Str;
    stream  << detail::widen<Ch>("<?xml version=\"1.0\" encoding=\"")
            << settings.encoding
            << detail::widen<Ch>("\"?>\n");
    write_xml_element(stream, Str(), pt, -1, settings);
    if (!stream)
        BOOST_PROPERTY_TREE_THROW(xml_parser_error("write error", filename, 0));
}
Herwig answered 17/4, 2013 at 11:7 Comment(3)
What do you mean with "it's not real"?Favian
@Favian that it's not possible.Herwig
ah, not possible. Well, it's very disappointing. I knew boost used rapidxml internally. I know that this is possible with rapidxml, I really don't understand why they did not give us this possibility. Thank you for your answer anyway.Favian
M
8

Yes, it is possible, but you'll need to call the function 'write_xml_element' directly. Here is an example with boost 1.49:

using namespace boost::property_tree;
ptree pt;
...
write_xml_element(std::cout,ptree::key_type(),pt,-1,xml_writer_settings<ptree::key_type::value_type>());

Of course. you can replace the standard output with std::ofstream or any other output stream you want.

Misconceive answered 30/3, 2016 at 11:14 Comment(1)
Note for anyone who stumbles across this answer: If you want to update this for boost in later versions (I'm on 1.69), just omit ::value_type in xml_writer_settings<> (so it's xml_writer_settings<ptree::key_type>()) - newer boost versions use the key_type to get the value_type. My code is just: write_xml_element(std::cout, {}, tree, -1, xml_writer_settings<ptree::key_type>{});. Thanks for your answer, helped me a lot!Zimmermann
H
1

No, it's not possible. look here for members of xml_writer_settings

And too, write_xml calls write_xml_internal that is (in boost 1.52)

template<class Ptree>
void write_xml_internal(
std::basic_ostream<typename Ptree::key_type::value_type> &stream, 
const Ptree &pt,
const std::string &filename,
const xml_writer_settings<typename Ptree::key_type::value_type> & settings)
{
    typedef typename Ptree::key_type::value_type Ch;
    typedef typename std::basic_string<Ch> Str;
    stream  << detail::widen<Ch>("<?xml version=\"1.0\" encoding=\"")
            << settings.encoding
            << detail::widen<Ch>("\"?>\n");
    write_xml_element(stream, Str(), pt, -1, settings);
    if (!stream)
        BOOST_PROPERTY_TREE_THROW(xml_parser_error("write error", filename, 0));
}
Herwig answered 17/4, 2013 at 11:7 Comment(3)
What do you mean with "it's not real"?Favian
@Favian that it's not possible.Herwig
ah, not possible. Well, it's very disappointing. I knew boost used rapidxml internally. I know that this is possible with rapidxml, I really don't understand why they did not give us this possibility. Thank you for your answer anyway.Favian
S
0

It seems that write_xml_element can't work without xml_writer_settings. And xml_writer_settings has two different incompatible versions; one with and another with .

In boost v1.58 this line is valid:

boost::property_tree::xml_writer_settings<std::string> settings;
boost::property_tree::xml_parser::write_xml_element(xmlStream,ptree::key_type(),pt_,-1, settings);

while in boost 1.54 these are valid:

boost::property_tree::xml_writer_settings<char> settings;
boost::property_tree::xml_parser::write_xml_element(xmlStream,ptree::key_type(),pt_,-1, settings);

I'm not sure if there is any solution for unifying them. So consider the boost version when working with write_xml_element.

Seagirt answered 27/9, 2017 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.