Search code examples
c++pugixml

assigning nodes to a new parent using pugixml


I use pugixml to read a svg document.

After I read the svg I have a pugixml::document like this (simplified):

<svg>
    <rect .../>
    <text ...> text </text>
    <line .../>
</svg

After reading I want to wrap some of the child nodes into a g element, like so:

<svg>
    <g ...>
        <rect .../>
        <text ...> text </text>
    </g>
    <line .../>
</svg>

How can I do this, preferably by modifying the pugixml::document in place and not creating a new one?


Solution

  • Something like this should work:

    pugi::xml_node g = svg.prepend_node("g");
    
    g.append_copy(svg.child("rect"));
    svg.remove_child("rect");
    
    g.append_copy(svg.child("text"));
    svg.remove_child("text");