gpt4 book ai didi

c - 从 XML 文件中提取数据

转载 作者:行者123 更新时间:2023-11-30 17:41:57 26 4
gpt4 key购买 nike

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

最佳答案

似乎 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

再见,

弗朗西斯

关于c - 从 XML 文件中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com