0

我使用 Mini-XML 在我的项目中保存和处理数据,

这是代码:

#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;
}

问题是当我得到整数值时,它的值为 0(不要得到“action_number”的值)。当我使用 mxmlGetText 函数处理字符串值时,此问题不存在!

4

1 回答 1

0

似乎默认行为mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);是将所有字段视为文本。试试这个看看发生了什么:

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);
}

MXML_INTEGER_CALLBACK如果只保存整数,最简单的解决方法是切换到回调。

如果您希望混合整数、文本和实数,您应该编写自己的回调。http://www.msweet.org/documentation/project3/Mini-XML.html#LOAD_CALLBACKS

再见,

弗朗西斯

于 2014-01-03T15:27:40.213 回答