c++ - Remove child nodes from parent - PugiXML -


<node>   <a>     <b id = "it_den"></b>   </a>   <a>     <b id = "en_ken"></b>   </a>   <a>     <b id = "it_ben"></b>   </a> </node> 

how can remove child node of <a></a> has child node <b></b> has attribute id not starts it using pugixml. result below:

<node>   <a>     <b id = "it_den"></b>   </a>   <a>     <b id = "it_ben"></b>   </a> </node> 

this tricky if want remove nodes while iterating (to keep code single-pass). here's 1 way it:

bool should_remove(pugi::xml_node node) {     const char* id = node.child("b").attribute("id").value();     return strncmp(id, "it_", 3) != 0; }  (pugi::xml_node child = doc.child("node").first_child(); child; ) {     pugi::xml_node next = child.next_sibling();      if (should_remove(child))         child.parent().remove_child(child);      child = next; } 

alternatively can use xpath , remove results:

pugi::xpath_node_set ns = doc.select_nodes("/node/a[b[not(starts-with(@id, 'it_'))]]");  (auto& n: ns)     n.node().parent().remove_child(n.node()); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -