TiXmlDocument doc("config.xml"); doc.LoadFile(); TiXmlElement* root = doc.RootElement(); TiXmlElement* firstChild = root->FirstChildElement(); TiXmlDocument* parentDoc = firstChild->GetDocument();
void printElement(TiXmlElement* element) { const TiXmlDocument* doc = element->GetDocument(); std::cout << "The parent document contains the following elements:" << std::endl; TiXmlElement* root = doc->RootElement(); for (TiXmlElement* child = root->FirstChildElement(); child != NULL; child = child->NextSiblingElement()) { std::cout << "\t" << child->Value() << std::endl; } } int main() { TiXmlDocument doc("config.xml"); doc.LoadFile(); TiXmlElement* root = doc.RootElement(); TiXmlElement* firstChild = root->FirstChildElement(); printElement(firstChild); return 0; }In this example, a helper function named printElement() is defined that takes a TiXmlElement pointer as an argument. The function first obtains the parent document of the element using GetDocument(), and then retrieves the root element of the document. It then iterates through all child elements of the root element, printing out their names. In the main() function, a TiXmlDocument is loaded from "config.xml". The root element is retrieved, and its first child element is passed to the printElement() function. Overall, these examples demonstrate the use of TiXmlElement::GetDocument() to obtain a pointer to the parent document of an element in TinyXML.