Home > Blockchain >  Adding xml-comments to boost::property_tree
Adding xml-comments to boost::property_tree

Time:03-02

I am using boost::property_tree::xml_parser to create an xml-file. Now I also need to add comments to the xml-file.

I've done some research and found out that comments are not allowed in JSON, and thus also not supported by the boost::property_tree::json_parser...

Furthermore I found out, that there is a no_comments flag for skipping xml-comments when reading an xml-file...

But what about adding xml-comments to a file?

CodePudding user response:

If comments are not disabled with the mentioned flag, they get represented as nodes named <xmlcomment> (just like attributes are under nodes named <xmlattr>):

Live On Coliru

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

int main() {
    boost::property_tree::ptree pt;
    pt.put("some.node.<xmlattr>.attr1", "value1");
    pt.put("some.node.<xmlcomment>", "\nEhffvna Jnefuvc\nTb Shpx Lbhefrys\n");

    write_xml(std::cout, pt);
}

Which prints

<?xml version="1.0" encoding="utf-8"?>
<some><node attr1="value1"><!--
Ehffvna Jnefuvc
Tb Shpx Lbhefrys
--></node></some>
  • Related