I’ve done a lot of research on this, and as far as I can tell, there’s only two ways to do it:
- Create the ENTIRE file manually in the form of a FString buffer.
- Using const_cast to remove the const from a pointer.
Option number one is a non-starter. If I have to do that, I’m using TinyXml2.
Option number two is better… slightly. One should never use const_cast if it can be avoided. Changing a variable that’s supposed to be const can lead to unexpected results (and therefore crashes). It seems to work fine for now, and can even save the FXmlFile to a file on the disk, but who knows what future results will be.
Why do I have to use const_cast? Because there’s no way to get the child/next node without it being const.
//From XmlNode.h
const FXmlNode* GetNextNode() const;
const FXmlNode* GetFirstChildNode() const;
There are no overloaded non-const functions to get them. Creating a child node doesn’t return anything:
//From XmlNode.h
void AppendChildNode(const FString& InTag, const FString& InContent);
The only way to get it would be to loop through to the last child node and grab it, but that gives me the original problem of const pointers, so I ended up using const_cast so I can forcibly manipulate them.
FXmlNode* nextNode = const_cast<FXmlNode*>(node->GetNextNode());
But that’s about the nastiest thing a programmer can do. This can lead to extreme problems if the nodes aren’t expecting values to change. It seems to work, but there’s not guarantee it’ll continue to work.
My question is: “Is there another way to do this that I haven’t found yet?”