gpt4 book ai didi

c - miniXML 解析 C API

转载 作者:数据小太阳 更新时间:2023-10-29 02:15:56 28 4
gpt4 key购买 nike

我正在尝试解析以下 XML 文件:

<root>Root
<pai>Pai_1
<filho>Pai1,Filho1</filho>
<filho>Pai1,Filho2</filho>
</pai>
<pai>Pai_2
<filho>Pai2,Filho1</filho>
<filho>Pai2,Filho2</filho>
</pai>
</root>

我正在使用以下 C 代码:

//... open file
xml_tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);

node = xml_tree;
printf("%s\n", mxmlGetText(node, NULL));
// here the return is: Root
// I expected: Root, OK

node = xml_tree->child;
printf("%s\n", mxmlGetText(node, NULL));
// here the return is: Root
// I expected: Pai_1, not OK

node = mxmlGetFirstChild(xml_tree);
printf("%s\n", mxmlGetText(node, NULL));
// here the return is: Root
// I expected: Pai_1, not OK

node = mxmlFindElement(xml_tree, xml_tree, "pai", NULL, NULL, MXML_DESCEND);
printf("%s\n", mxmlGetText(node, NULL));
// here the return is: Pai_1
// I expected: Pai_1, OK

node = mxmlGetNextSibling(node);
printf("%s\n", mxmlGetText(node, NULL));
// here the return is: (NULL)
// I expected: Pai_2, not OK

我怎样才能访问根的 child ?我的访问概念哪里错了?

谢谢。


在@RutgersMike 回复后编辑

我扩展你的 while 循环来尝试理解 minixml 的概念:

root = mxmlLoadFile(NULL,fp,MXML_TEXT_CALLBACK);
node = root;

printf("------- Root\n");
fprintf(stdout,"Element = %s\n",mxmlGetElement(node));
fprintf(stdout," Value = %s\n",mxmlGetText(node,0));
printf("\n");

printf("------- First child of Root\n");
node = mxmlGetFirstChild(node);
fprintf(stdout,"Element = %s\n",mxmlGetElement(node));
fprintf(stdout," Value = %s\n",mxmlGetText(node,0));
printf("\n");

printf("------- Sibling 1 of First child of Root\n");
node = mxmlGetNextSibling(node);
fprintf(stdout,"Element = %s\n",mxmlGetElement(node));
fprintf(stdout," Value = %s\n",mxmlGetText(node,0));
printf("\n");

printf("------- Sibling 2 of First child of Root\n");
node = mxmlGetNextSibling(node);
fprintf(stdout,"Element = %s\n",mxmlGetElement(node));
fprintf(stdout," Value = %s\n",mxmlGetText(node,0));
printf("\n");

printf("------- Sibling 3 of First child of Root\n");
node = mxmlGetNextSibling(node);
fprintf(stdout,"Element = %s\n",mxmlGetElement(node));
fprintf(stdout," Value = %s\n",mxmlGetText(node,0));
printf("\n");

printf("------- Sibling 4 of First child of Root\n");
node = mxmlGetNextSibling(node);
fprintf(stdout,"Element = %s\n",mxmlGetElement(node));
fprintf(stdout," Value = %s\n",mxmlGetText(node,0));
printf("\n");

结果是:

------- Root
Element = root
Value = Root

------- First child of Root
Element = (null)
Value = Root

------- Sibling 1 of First child of Root
Element = (null)
Value =

------- Sibling 2 of First child of Root
Element = pai
Value = Pai_1

------- Sibling 3 of First child of Root
Element = (null)
Value =

------- Sibling 4 of First child of Root
Element = pai
Value = Pai_2

我认为这种在子进程和父进程之间导航的概念有点奇怪。为什么 sibling 之间有(空)值?

我正在考虑回到 ezxml。

谢谢

最佳答案

我也刚开始玩 min-xml 虽然我一直因为缺乏好的例子而感到非常沮丧,但我借用并增强了一个不错但不完美的阅读 XML 的例子文件并查看所有部分。它显示标签名称、属性和标签之间的文本值。不确定如何识别结束标记。确保标准 xml 标记位于 xml 文件的顶部。包括stdio, stdlib, string .h 文件。

#include "mxml.h"

int main (int argc, char **argv ) {

FILE *fp = NULL;

int k = 0;

mxml_node_t * tree = NULL;
mxml_node_t * node = NULL;

if (argc < 2){
perror("Argument Required XML File ");
exit(1);
}
fp = fopen (argv[1], "r");
if (fp ){
tree = mxmlLoadFile (NULL , fp , MXML_OPAQUE_CALLBACK);
}else {
perror("Could Not Open the File Provided");
exit(1);
}
if (tree){
for (node = mxmlFindElement(tree, tree,NULL,NULL, NULL,MXML_DESCEND);
node != NULL;
node=mxmlWalkNext (node, NULL, MXML_DESCEND)
//node = mxmlFindElement(node, tree, NULL,NULL,NULL,MXML_DESCEND)
){
if (node->type == MXML_ELEMENT) {
printf("MXML_ELEMENT Node <%s>:%d \n", node->value.element.name, node->value.element.num_attrs);
for (k = 0; k < node->value.element.num_attrs; k++){
if (node->value.element.attrs ){
printf ("Attribute Name :: %s \n", node->value.element.attrs[k].name);
printf ("Attribute Value:: %s \n", node->value.element.attrs[k].value);
}
//if (!strncmp(node->value.element.name , "display-name", 12 )){
// printf(" String %s \n", (char*) node->child->value.text.string);
//}
}
}
else if (node->type == MXML_REAL){
printf("MXML_REAL Node is %s \n", node->value.element.name);
}
else if(node->type == MXML_OPAQUE){
printf("MXML_OPAQUE Node is %s \n", node->value.element.name);
}
else if(node->type == MXML_INTEGER){
printf("MXML_INTEGER Node is %s \n", node->value.element.name);
}
else if(node->type == MXML_TEXT){
printf("MXML_TEXT Node is %s \n", node->value.element.name);
}
else if(node->type == MXML_IGNORE){
printf("MXML_IGNORE Node is %s \n", node->value.element.name);
}
else if(node->type == MXML_CUSTOM){
printf("MXML_IGNORE Node is %s \n", node->value.element.name);
}
else {
printf("Type Default Node is %s \n", node->value.element.name);
}
}
}
if (tree){
mxmlDelete(tree);
}
if (fp){
fclose(fp);
}
return 0;
}

关于c - miniXML 解析 C API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9806996/

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