3

For example I have boost property tree of following structure (created by reading stream with xml or in different way):

<A>
  <B>
    <C></C>
  </B>
</A>

How to rename in existing tree element B to new element with new key: N. So invoking write_xml of this fixed tree should give new xml structure:

<A>
  <N>
    <C></C>
  </N>
</A>

Please present code if it is possible or explain why it is not. Remark: attaching subtree under C to newly generated root is also acceptable but direct renaming in priority.

sehe
  • 374,641
  • 47
  • 450
  • 633
Vlad
  • 1,977
  • 19
  • 44
  • 1
    How much is the pay? – sehe Jun 03 '15 at 23:38
  • question eligible for bounty in 2 days. for money I usually code myself. – Vlad Jun 04 '15 at 00:44
  • I was jabbing at the fact that you show zero code yourself. And the abundance of dryly asserted requirements made this look like work. Anyhoops, no need for more imaginary internet points :) Cheers – sehe Jun 04 '15 at 00:54

1 Answers1

3

Well, then, it is possible. Send check for code

Live On Coliru

#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);
}

Prints

<?xml version="1.0" encoding="utf-8"?>
<A><N><C/></N></A>
sehe
  • 374,641
  • 47
  • 450
  • 633
  • I think there are some comment & chat-related badges ([Pundit](http://stackoverflow.com/help/badges/94/pundit) comes to mind) – sehe Jun 04 '15 at 00:55
  • is that incurring a (deep) copy of A.B to A.N before discarding A.B? If so, is there a version with move semantics? – gg99 Apr 22 '20 at 22:22
  • 1
    @gg99 Boost Property Tree is not move-aware. (Too old). With the underlying containers (Boost Multi-Index) you can achieve the effect in a variety of elegant ways, but that requires going into the implementation details of this library. However, none of this matters as Boost Property Tree is both not designed for and not suited for anything performance-sensitive. When people are tempted to do low-lat things with it, invariably they should be using a good JSON or XML library instead. – sehe Apr 23 '20 at 00:15