次の 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
この子と親の間のナビゲーションの概念は少し奇妙だと思います。兄弟間に(null)値があるのはなぜですか?
私はezxmlに戻ることを検討しています。
ありがとうございました