2

My XML looks like this:

<root>
<sets>
    <childSets>
        <childs>
            <child>
                <childId>11</childId>
        </child>
        <child>
            <childId>22</childId>
        </child>
        <child>
            <childId>33</childId>
        </child>
        <child>
            <childId>44</childId>
        </child>
[...]
    </childs>
</childSets>

    <childSets>
[...]
    </childSets>
</sets>
</root>

I want to pars all childId elements of child.

Actually, the result of my parsing is just the first childId of every childs element:

[...]
do {
  if ([[TBXML elementName:element] isEqualToString:@"childSet"]) {
  [...]
        TBXMLElement *xmlChildId        = [TBXML childElementNamed:@"childId"           parentElement:child];
  [...]
  }
} while ((element = element->nextSibling));  
[...]

In this case, I just get 11. Whats wrong here?

EDIT 1:

The solution is to find the first child of the child element and to parse the next elements of this, here it is:

This code is in the do while loop of the method traverseElement

do { [...]
        if ([[TBXML elementName:element] isEqualToString:@"childSet"]) {
        //find the first child
            if ((ChildPARENTELEMENT->firstChild)) {

            while (ChildOfCHILDELEMENT!=nil) {
                NSLog(@"....");

                 ChildOfCHILDELEMENT = [TBXML nextSiblingNamed:@"childs" searchFromElement:ChildOfCHILDELEMENT];

            }
        }
    }
while((element = element->nextSibling));
brush51
  • 5,691
  • 6
  • 39
  • 73
  • Yes, the second do while crashes the app. i dont know how to solve the second loop. Can you give me an advice? – brush51 Sep 14 '11 at 19:52
  • Please include more information on the error you are seeing. Are you getting a SIGABRT? Does app close with no logs / debugging info? If there is debugging info, what line is crashing and with what error message? – Sam Sep 15 '11 at 18:05
  • Hello Sam, thank you for your help, i have included my solution, i solved it yesterday night :) i cant remember if it was a SIGABRT or EXC_BAD_ACCESS but the error appears in the second loop condition. – brush51 Sep 16 '11 at 08:20

2 Answers2

4

The solution is to find the first child of the child element and to parse the next elements of this, here it is:

This code is in the do while loop of the method traverseElement

do { [...]
    if ([[TBXML elementName:element] isEqualToString:@"childSet"]) {
    //find the first child
        if ((ChildPARENTELEMENT->firstChild)) {

        while (ChildOfCHILDELEMENT!=nil) {
            NSLog(@"....");

             ChildOfCHILDELEMENT = [TBXML nextSiblingNamed:@"childs" searchFromElement:ChildOfCHILDELEMENT];

        }
    }
}
while((element = element->nextSibling));
brush51
  • 5,691
  • 6
  • 39
  • 73
0

You need an inner loop that goes through all the child xml elements. The childElementName:parentElement: will only return the first child with that name.

Try something like:

[...]
do {
   if ([[TBXML elementName:element] isEqualToString:@"childSet"]) {
      [...]
      TBXMLElement *xmlChildId = [TBXML childElementNamed:@"childId" 
                                            parentElement:child];
      do {
         // Do nslog or whatever else on xmlChildId
         [...]

         // Find next child element to process
         xmlChildId = [TBXML nextSiblingName:@"childId" 
                           searchFromElement:xmlChildId];
      } while (xmlChildId != nil);
   }
} while ((element = element->nextSibling));  
[...]
Sam
  • 26,946
  • 12
  • 75
  • 101
  • Did this help you? If not, please share more code. There are quite a few [...] in there. – Sam Sep 13 '11 at 22:27
  • no it doesnt help. the loop crashes the app. the [...] are logs and the same parsing lines with just other childelements. – brush51 Sep 14 '11 at 19:56
  • You adviced me in the right direction, thank you very much. I have solved it and my code is up there. Thanks again. – brush51 Sep 16 '11 at 08:20
  • Can you post your solution and mark it as the answer? It's OK to post an answer to your own question and mark it as the answer (see [FAQ](http://stackoverflow.com/faq#questions)). This helps other people with the same problem more easily identify the solution. Glad you got everything worked out :) – Sam Sep 16 '11 at 14:25
  • In case anyone else has a similar issue: I had a working Do/While loop - but only if there was no extra XML data below the area I wanted to extract data from - if there was the app would bomb out with a nil value (it kept looping and used the next value). Adding the inner IF TBXML element isEqualToString:@"myString" stopped the crash. Thx – user3741598 Mar 20 '20 at 03:23