boost::property_tree::ptree with write_xml
All, I am looking got the help with xml writer using property tree. My requirement is to write a user defined comment (in below example /*comment*/) in every line of XML file. Example: /*comment*/<debug> /*comment*/ <filename>debug.log</filename> /*comment*/ <modules> /*comment*/ <module>Finance</module> /*comment*/ <module>Admin</module> /*comment*/ <module>HR</module> /*comment*/ </modules> /*comment*/ <level>2</level> /*comment*/</debug> Can someone help me in solving the above? Any help on this would be highly appreciated. boost::property_tree::xml_writer_settings<std::string> w(' ', 4); write_xml(os, pt,w); Regards, UJ
On Wed, Mar 18, 2015 at 6:18 AM, Uthpal Urubail <uthpal.urubail@altair.com> wrote:
All,
I am looking got the help with xml writer using property tree.
My requirement is to write a user defined comment (in below example /*comment*/) in every line of XML file.
I don't know about the property tree writer, but that's an invalid Xml comment. http://www.w3.org/TR/REC-xml/#sec-comments
Example:
/*comment*/<debug>
/*comment*/ <filename>debug.log</filename>
/*comment*/ <modules>
/*comment*/ <module>Finance</module>
/*comment*/ <module>Admin</module>
/*comment*/ <module>HR</module>
/*comment*/ </modules>
/*comment*/ <level>2</level>
/*comment*/</debug>
Can someone help me in solving the above? Any help on this would be highly appreciated.
boost::property_tree::xml_writer_settings<std::string> w(' ', 4);
write_xml(os, pt,w);
Regards,
UJ
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Unfortunately my requirement is to add comments to the beginning of the line. I understand this makes invalid XML. I don't know about the property tree writer, but that's an invalid Xml comment. http://www.w3.org/TR/REC-xml/#sec-comments Regards, UJ
Hi, I don't know how to solve it with pure Boost.PropertyTree, but using Boost.Iostreams it can be done quite easy. Example below, hope it suits your need. Configuring XML-formatting itself I leave up to you. #include <boost/iostreams/device/file.hpp> #include <boost/iostreams/filter/line.hpp> #include <boost/iostreams/filtering_stream.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> namespace io = boost::iostreams; class prefix_front_pusher: public io::line_filter /* may use wline_filter */ { public: using io::line_filter::string_type; explicit prefix_front_pusher(const string_type &prefix): m_prefix(prefix) {} private: string_type do_filter(const string_type &line) override { return m_prefix + line; } const string_type m_prefix; }; int main() { io::filtering_ostream out; out.push(prefix_front_pusher("/* my prefix */")); out.push(io::file_sink("my_file.txt")); boost::property_tree::ptree ptree; ptree.put("hello", "world"); boost::property_tree::write_xml(out, ptree); } On Thu, Mar 19, 2015 at 6:10 AM Uthpal Urubail <uthpal.urubail@altair.com> wrote:
Unfortunately my requirement is to add comments to the beginning of the line. I understand this makes invalid XML.
I don't know about the property tree writer, but that's an invalid Xml comment. http://www.w3.org/TR/REC-xml/#sec-comments
Regards, UJ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Michael Powell
-
Uthpal Urubail
-
Алексей Филиппов