我使用 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 函数处理字符串值时,此问题不存在!