Talk:Mini-XML
This is an old revision of this page, as edited by Freezy (talk | contribs) at 11:24, 20 August 2009. It may differ significantly from the current revision. |
Loading from an XML With default values to fall back on
I found the following code usefull, please note using this demands you check for NULL yourself! This should not pose a problem, as Name is most likely a constant string.
Loading settings from a XML with these is easier, as you don't have to check and correct manually in the loading code.
#include <mxml.h> //watch out for unsafe code! //USE WITH CAUTION!!! int mxml_GetInt(mxml_node_s * node, const char * name, int default_value) { const char * value = mxmlElementGetAttr(node,name); if(value) { return atoi(value); } return default_value; } //watch out for unsafe code! //USE WITH CAUTION!!! // use strcpy if you want to keep the string (the result is a char[] that is replaced or freed) const char * mxml_GetString(mxml_node_s * node, const char * name, char * default_value) { const char * value = mxmlElementGetAttr(node,name); if(value) { return value; } return default_value; }
Freezy 09:23, 20 August 2009 (UTC)