My XML looks like this:
<root>
<sets>
<childSets>
<childs>
<child>
<childId>11</childId>
</child>
<child>
<childId>22</childId>
</child>
开发者_StackOverflow中文版 <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));
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));
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));
[...]
精彩评论