xml - C++ pugiXML, Append a child before the first child in a node -


how append new child node , place prior first child? i.e. want try , append new child , push top in order.

say, if have:

pugi::xml_node root;  pugi::xml_node level1 = root.append_child("level1"); pugi::xml_node level2 = root.append_child("level2"); pugi::xml_node level3 = root.append_child("level3"); 

can somehow append new node, level4 , have before level1 node in xml tree?

you can use root.insert_child_before("level4", root.first_child()).

its rater unusual have different tag name each child though. more common format have children same name , set attributes differentiate them 1 another.

an example of how can done:

int main() {     pugi::xml_document doc;     pugi::xml_node root = doc.append_child("levels");      root.append_child("level").append_attribute("id").set_value("l01");     root.last_child().append_child("description").text().set("some l01 stuff");      root.append_child("level").append_attribute("id").set_value("l02");     root.last_child().append_child("description").text().set("some l02 stuff");      // insert 1 before first child     root.insert_child_before("level", root.first_child()).append_attribute("id").set_value("l00");     root.first_child().append_child("description").text().set("some l00 stuff");      doc.print(std::cout); } 

output:

<levels>     <level id="l00">         <description>some l00 stuff</description>     </level>     <level id="l01">         <description>some l01 stuff</description>     </level>     <level id="l02">         <description>some l02 stuff</description>     </level> </levels> 

Comments