从父级删除子节点 - PugiXML

时间:2016-05-25 03:43:52

标签: c++ pugixml

ALTER SEQUENCE YOUR_SEQUENCE_NAE INCREMENT BY 50;

如何使用PugiXML删除 @SequenceGenerator(name = "YOUR_ID_GEN", sequenceName = "SEQ_YOUR_ID", allocationSize=50) @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "YOUR_ID_GEN") @Column(name = "YOUR_ID") public Long getYourId() { return this.yourId; } 的子节点,该子节点的子节点<Node> <A> <B id = "it_DEN"></B> </A> <A> <B id = "en_KEN"></B> </A> <A> <B id = "it_BEN"></B> </A> </Node> 的属性<A></A>不是以 <B></B> 开头。 结果如下:

id

2 个答案:

答案 0 :(得分:4)

如果要在迭代时删除节点(以保持代码单遍),这有点棘手。这是一种方法:

bool should_remove(pugi::xml_node node)
{
    const char* id = node.child("B").attribute("id").value();
    return strncmp(id, "it_", 3) != 0;
}

for (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;
}

或者,您可以使用XPath并删除结果:

pugi::xpath_node_set ns = doc.select_nodes("/Node/A[B[not(starts-with(@id, 'it_'))]]");

for (auto& n: ns)
    n.node().parent().remove_child(n.node());

答案 1 :(得分:0)

另一种方法是在删除子级之前增加迭代器。要在迭代时删除属性。

for(pugi::xml_attribute_iterator it = node.attributes_begin(); it != node.attributes_end();){
    pugi::xml_attribute attr = *it++;
    if(should_remove(attr)){
        node.remove_attribute(attr);
    }
}