Я пытаюсь разобрать следующий 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
Как получить доступ к дочернему элементу root?Где моя концепция доступа неверна?
Спасибо.
РЕДАКТИРОВАТЬ после ответа @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.
Спасибо