C++pugiXML,在节点的第一个子节点之前附加一个子节点
C++ pugiXML, Append a child before the first child in a node
如何将新的子节点附加到节点并将其放置在第一个子节点之前?即,我想尝试附加一个新的子项,并按顺序将其推到顶部。
说,如果我有:
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");
我可以以某种方式附加一个新节点level4
并将其放在XML树中的level1
节点之前吗?
您可以使用root.insert_child_before("Level4", root.first_child())
。
然而,每个孩子都有一个不同的标签名,这是不寻常的。一种更常见的格式是让所有子项都具有相同的名称,并设置属性以将它们彼此区分开来。
如何做到这一点的一个例子:
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");
// now insert one before the 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);
}
输出:
<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>
有人刚刚让我做prepend_child
。不过,还是要感谢Galik的建议。
相关文章:
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 删除 XML 文件中的子节点C++
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 语言分析的二叉树实现 - 子节点:不起作用
- Node中的子节点列表在C++中的BFS遍历过程中丢失
- c++ 无法从 XML 获取所有子节点
- 使用访问者设计模式在N- ARY树中重复访问子节点值
- TinyXML2 从节点和所有子节点获取文本
- 遍历仅引用每个节点的子节点的树
- 从父节点中删除子节点 - PugiXML
- 根据其子节点的余额计算 AVL 树节点余额
- 有没有一种方便的方法可以从属性树中删除节点,同时保留其子节点
- 访问BST子节点时出现分段故障.为什么?
- 使用节点指针数组来指向节点的子节点
- 如何使链表具有子节点
- 如何获取所有子节点值
- 解析 AST 中的节点及其自己的(无限数量的嵌套)子节点
- 无法从具有 MSXML 的子节点获取值 C++
- QTreeView 使用示例 - 如何将子节点添加到现有项目
- 如何使用OpenCV库访问xml文件中的子节点