// Include the required headers for parsing XML documents #includeusing namespace tinyxml2; int main() { // Define a new XML document XMLDocument xmlDoc; // Create the first element and set its value XMLElement* pRoot = xmlDoc.NewElement("root"); xmlDoc.InsertFirstChild(pRoot); // Create the child element and set its value XMLElement* pChild = xmlDoc.NewElement("child"); pChild->SetAttribute("name", "John"); pChild->SetAttribute("age", "25"); // Add the child element to the root element pRoot->InsertEndChild(pChild); // Save the XML document to a file xmlDoc.SaveFile("example.xml"); return 0; }
// Include the required headers for parsing XML documents #includeIn this example, we create an XML document with a root element called "root". We then create two child elements, "level1" and "level2", and set the value of the second level child element. This example demonstrates how to use CreateChildElement to create nested child elements. Package library: pugixml.using namespace pugi; int main() { // Create an XML document xml_document xmlDoc; // Create the root element xml_node root = xmlDoc.append_child("root"); // Create the first level child element xml_node level1 = root.append_child("level1"); // Create the second level child element xml_node level2 = level1.append_child("level2"); // Set the value of the second level child element level2.text().set("This is a test"); // Save the XML document to a file xmlDoc.save_file("example.xml"); return 0; }