C++ 中有许多库可以用来生成 XML 文档,其中比较流行的有 TinyXML、pugixml 和 RapidXML。这里以使用 pugixml 库为例说明如何生成 XML 文档。
#include "pugixml.hpp" 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 文档。当然,你也可以使用其他库来实现类似的功能。