#include "tinyxml.h" #includeint main() { TiXmlDocument doc("example.xml"); if(!doc.LoadFile()) { std::cerr << "Error loading XML file." << std::endl; return -1; } TiXmlNode* root = doc.FirstChild("root"); if(root) { for(TiXmlNode* node = root->FirstChild(); node; node = node->NextSibling()) { std::cout << node->Value() << std::endl; } } return 0; }
#include "tinyxml.h" #includeIn this example, we load an XML document, find the "child2" element and modify its "id" attribute. We use the TiXmlNode class to traverse the document, cast the child node to the TiXmlElement class and to modify the element's attribute with the SetAttribute() method. Finally, we save the modified document to a new XML file. Package library: The TiXmlNode class is part of the TinyXML package, which is available for download at https://sourceforge.net/projects/tinyxml/.int main() { TiXmlDocument doc("example.xml"); if(!doc.LoadFile()) { std::cerr << "Error loading XML file." << std::endl; return -1; } TiXmlNode* root = doc.FirstChild("root"); if(root) { for(TiXmlNode* node = root->FirstChild(); node; node = node->NextSibling()) { if(node->Value() == "child2") { TiXmlElement* element = node->ToElement(); if(element) { element->SetAttribute("id", "new_id"); } } } } doc.SaveFile("modified.xml"); return 0; }