我正在尝试解析以下 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
如何访问根的孩子?我的访问概念在哪里是错误的?
谢谢你。
在@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
我觉得这种在孩子和父母之间导航的概念有点奇怪。为什么兄弟姐妹之间有(空)值?
我正在考虑回到ezxml。
谢谢