0

I use Mini-XML to save and handle data in my project,

this is the code :

#include <microxml.h>

int main() 
{
    FILE *fp;
    mxml_node_t *tree, *data, *b;
    int action_num1;

    fp = fopen(FILE_PATH, "r");

    if (fp!=NULL) {
        tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
        if (!tree) return -1;
        fclose(fp);
    }
    else return -1;

    data = mxmlFindElement(tree, tree, "first_node", NULL, NULL, MXML_DESCEND);
    if (!data) goto error;
    fp = fopen(FILE_PATH, "w");
    if (fp!=NULL) {
        b = mxmlNewElement(data, "action");
        if (!b) goto error;
        n = b;
        b = mxmlNewElement(n, "action_number");
        if (!b) goto error;
        b = mxmlNewInteger(b, 123);
        if (!b) goto error;
        b = mxmlNewElement(n, "action_type");
        if (!b) goto error;
        b = mxmlNewInteger(b, 1);
        if (!b) goto error;

        mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
        fclose(fp);
        mxmlDelete(tree);
        return 0;
    }
    fp = fopen(FILE_PATH, "r");
    if (fp!=NULL) {
        tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
        if (!tree) return -1;
        fclose(fp);
    }
    else return -1;

    data = mxmlFindElement(tree, tree, "first_node", NULL, NULL, MXML_DESCEND);
    if (!data) goto error;

    b = data;
    while (b) {
        if (b->type == MXML_ELEMENT &&
            !strcmp(b->value.element.name, "action")) {

            c = mxmlFindElement(b, b, "action_number",NULL, NULL, MXML_DESCEND);
            if (!c) goto error1;
            if(c->type == MXML_ELEMENT &&
                       c->child
                   c->child->type == MXML_INTEGER) {
                action_num1 = mxmlGetInteger(c); `// **===> get integer value**` 
            }
      }
    b = mxmlWalkNext(b, data, MXML_DESCEND);
    }
error:
    close(fp);
    mxmlDelete(tree);
    return -1;

error1:
    mxmlDelete(tree);
    return -1;
}

the problem is when I get integer value it's value is 0 (don't get the value of "action_number"). this issue is not present when I use mxmlGetText function to handle string value !

Anis_Stack
  • 3,306
  • 4
  • 30
  • 53

1 Answers1

0

It seems that the default behavior of mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); is to consider all fields as text. Try this to see what happened :

printf("%d %d %d %d %d \n",c->type, MXML_INTEGER, MXML_ELEMENT,MXML_TEXT,c->child->type);       
if(c->type == MXML_ELEMENT ) {
   char* bla=mxmlGetText(c,NULL);
   action_num1 = mxmlGetInteger(c); // **===> get integer value**
   printf("found one integer %d %s\n",action_num1,bla);
}

The easiest fix is to switch to the callback MXML_INTEGER_CALLBACK if you only save integers.

If you wish to mix integers, text and real, you should write your own callback. http://www.msweet.org/documentation/project3/Mini-XML.html#LOAD_CALLBACKS

Bye,

Francis

francis
  • 9,525
  • 2
  • 25
  • 41