C++ 中有许多库可以用来生成 XML 文档,其中比较流行的有 TinyXML、pugixml 和 RapidXML。这里以使用 pugixml 库为例说明如何生成 XML 文档。
首先,你需要在代码中包含 pugixml 头文件:#include "pugixml.hpp"创建一个 XML 文档对象:pugi::xml_document doc;创建根节点并将其添加到文档中:pugi::xml_node root = doc.append_child("root");添加子节点和属性:pugi::xml_node child = root.append_child("child");child.append_attribute("name") = "John";child.append_attribute("age") = 30;保存文档到文件或字符串:doc.save_file("output.xml");// 或者std::stringstream ss;doc.save(ss);std::string xmlString = ss.str();通过这些步骤,你就可以使用 pugixml 库生成 XML 文档。当然,你也可以使用其他库来实现类似的功能。


