我使用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函数来处理字符串值时,这个问题不存在!
答案 0 :(得分: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
再见,
弗朗西斯