pugixml 快速新建节点
时间: 2023-07-01 07:04:34 浏览: 71
在使用pugixml创建XML节点时,可以使用`append_child`函数快速创建一个新节点并将其添加到父节点中。例如,以下代码创建了一个名为`bookstore`的根节点,然后创建了两个子节点`book`和`author`,并将它们添加到`bookstore`节点中:
```c++
#include "pugixml.hpp"
int main()
{
pugi::xml_document doc;
pugi::xml_node bookstore = doc.append_child("bookstore");
pugi::xml_node book = bookstore.append_child("book");
book.append_child("title").text() = "The Catcher in the Rye";
book.append_child("author").text() = "J.D. Salinger";
pugi::xml_node author = bookstore.append_child("author");
author.append_child("name").text() = "J.D. Salinger";
author.append_child("birthdate").text() = "January 1, 1919";
doc.save_file("example.xml");
return 0;
}
```
在上面的代码中,`append_child`函数的参数是要创建的节点的名称,然后可以使用返回的节点对象来添加子节点或设置节点的文本内容。最后,调用`save_file`函数将文档保存到文件中。