In TinyXml
you could create an Element
e.g. TiXmlElement("tag")
,
but in TinyXml2
there is no public constructor for XMLElement
?
How do you create elements ?
In TinyXml
you could create an Element
e.g. TiXmlElement("tag")
,
but in TinyXml2
there is no public constructor for XMLElement
?
How do you create elements ?
Similar to the existing answer, I wrote this helper utility for my application:
tinyxml2::XMLElement* CChristianLifeMinistryEntry::InsertNewElement(tinyxml2::XMLDocument& rDoc, tinyxml2::XMLElement*& pParent, LPCSTR strElement, CString strValue)
{
XMLElement *pElement = rDoc.NewElement(strElement);
USES_CONVERSION;
if (pElement == nullptr)
AfxThrowMemoryException();
pElement->SetText(CT2CA(strValue, CP_UTF8));
pParent->InsertEndChild(pElement);
return pElement;
}
It automatically adds a new child element to the end of the list. In addition, it sets the text value for the element.
You create an element within the context of the document, so call
tinyxml2::XMLElement * tinyxml2::XMLDocument::NewElement (const char * name)
.
E.g. to create a new element and add it as a child of an existing element e
XMLElement * new = e -> GetDocument() -> NewElement ("tag");
e -> InsertFirstChild (new);
Or, to do it in a single step, you could look up append_element
in my tinyxml2 extension