나는 추적의 XML 파일을 구문 분석하려고 해요 :miniXML 파싱 C의 API
<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 응답 후
는편집
나는 minixml의 개념을 이해하려고하기 위해 while 루프를 확장 :이이었다
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로 돌아갈 것을 고려하고있다.
는 자식 노드를 얻기 위해 여기 ( http://www.minixml.org/mxml.html#3_7)에 설명 된 반복 기능을 사용하고자하는 것 같습니다
예제 작업을 수행 할 수 없습니다. –
컴파일되지 않았거나 예상 한 결과가 나오지 않았습니까? 무엇을 생산 했습니까? – rutgersmike
I이 시퀀스를 수행 '노드 = xml_tree 단계;' '노드 = mxmlGetFirstChild (xml_tree)' '노드 = mxmlWalkNext (노드 xml_tree, MXML_DESCEND)' '의 printf ("> % S \ n"mxmlGetElement을 (node)); printf ("> % s \ n", mxmlGetText (node, NULL)); ' 결과 : (null) –