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
Post a Comment