TiXmlDocument doc("myxmlfile.xml"); doc.LoadFile(); // Get the root element of the XML document TiXmlElement* rootElement = doc.RootElement(); // Create a new child element TiXmlElement* newElement = new TiXmlElement("newelement"); // Add the new element as a child to the root element rootElement->LinkEndChild(newElement); // Save the modified XML document to a file doc.SaveFile();
TiXmlDocument doc; TiXmlElement* rootElement = new TiXmlElement("root"); doc.LinkEndChild(rootElement); TiXmlElement* childElement1 = new TiXmlElement("child1"); TiXmlText* childText1 = new TiXmlText("This is child element 1"); childElement1->LinkEndChild(childText1); rootElement->LinkEndChild(childElement1); TiXmlElement* childElement2 = new TiXmlElement("child2"); TiXmlText* childText2 = new TiXmlText("This is child element 2"); childElement2->LinkEndChild(childText2); rootElement->LinkEndChild(childElement2); doc.SaveFile("newxmlfile.xml");In this example, we create a new XML document without loading it from a file. We create a root element using TiXmlElement constructor, and add it to the document using LinkEndChild function. Then, we create two child elements with their corresponding text and add them as children to the root element using LinkEndChild function. Finally, we save the new XML document to a file. Package Library: TinyXML.