Hi, I want to save/load some custom data not stored in nodes to/from xml/json, how to do that?
Writing custom data to xml or json
Hello,
Nodes have a freeform VariantMap for persistent user variables; cf. class Node, SetVar/GetVar/GetVars methods.
Some examples of node vars usage can be found in the forum and samples 03,14, 24, 37.
@cadaver explains some about the VariantMap.
If you just want to create a custom xml/json file rather than storing data in the scene you can store the data in an XMLElement or JSONValue and then write it to an XMLFile or JSONFile.
Depending on your case you may also use a custom class that inherits from Serializable
and register attributes to it. By default these attributes will be saved and loaded with the SaveXML
and LoadXML
functions, which can be overridden for more control.
Check out the documentation on serialization for more information.
Thanks guys for replies, I think, the most related one is from @SirNate0, I wanted to do this, but I dont know how to append XMLElement to file:
//container for all items
XMLElement container = XMLElement();
//now suppose I want to add some thing
container.CreateChild("PI").SetFloat("value", 3.14f);
XMLFile * file = new XMLFile(context_);
//how to append container to file???
file->SaveFile("save.xml");
I don’t have a computer right now to check that this works, but I think what you want is XMLFile::
CreateRoot (const String &name)
Something like:
XMLFile * file = new XMLFile(context_);
//container for all items - the root element of the file
XMLElement container = file->CreateRoot("tagName");
//now suppose I want to add some thing
container.CreateChild("PI").SetFloat("value", 3.14f);
// Assuming I'm correct, the XMLFile should already have everything at this point and you just need to save it to the disk now
file->SaveFile("save.xml");
Thanks, works like a charm.