How to rename node / element in boost property tree?
For example, I have a boost property tree of the following structure (created by reading the stream with xml or otherwise):
<A>
<B>
<C></C>
</B>
</A>
How to rename in a tree item an existing new item with a new key: N. Thus, when calling write_xml of this fixed tree, a new xml structure must be specified:
<A>
<N>
<C></C>
</N>
</A>
Please provide the code if possible or explain why it is not. Note: joining a subtree under C to the newly created root is also acceptable, but direct renaming takes precedence.
+3
source to share
1 answer
Well, then it's possible. Submit code verification
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
int main() {
std::istringstream iss(R"(<A><B><C></C></B></A>)");
ptree pt;
read_xml(iss, pt);
pt.add_child("A.N", pt.get_child("A.B"));
pt.get_child("A").erase("B");
write_xml(std::cout, pt);
}
Printing
<?xml version="1.0" encoding="utf-8"?>
<A><N><C/></N></A>
+4
source to share