#include#include int main() { TiXmlDocument doc; TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "", ""); doc.LinkEndChild(decl); TiXmlElement *root = new TiXmlElement("root"); doc.LinkEndChild(root); TiXmlElement *child1 = new TiXmlElement("child1"); root->LinkEndChild(child1); TiXmlElement *child2 = new TiXmlElement("child2"); root->LinkEndChild(child2); for (TiXmlElement *elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) { std::cout << elem->Value() << std::endl; } return 0; }
#includeIn this example, we load an XML file into a TiXmlDocument object and retrieve the root element. We then iterate over the sub-elements of the root element and print the name of each element to the console. We also iterate over the attributes of each element using another for loop and print each attribute name and value to the console.#include int main() { TiXmlDocument doc; doc.LoadFile("example.xml"); TiXmlElement *root = doc.FirstChildElement("root"); if (root != NULL) { for (TiXmlElement *elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) { std::cout << "Element: " << elem->Value() << std::endl; for (TiXmlAttribute *attr = elem->FirstAttribute(); attr != NULL; attr = attr->Next()) { std::cout << "Attribute: " << attr->Name() << " = " << attr->Value() << std::endl; } } } return 0; }