Reputation: 766
I'm trying to parse the response from a web server.
My parser does as follows:
#import "XMLParser.h"
#import "AppDelegate.h"
#import "Child.h"
@implementation XMLParser
- (XMLParser *) initXMLParser {
self = [super init];
if(self)
{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"GetKidsResult"])
{
// initialize the array
if(!appDelegate.children)
{
appDelegate.children = [[NSMutableArray alloc] init];
}
}
else if([elementName isEqualToString:@"a:KeyValueOfintKidf4KEWLbb"])
{
if(!aChild)
{
//Initialize the child.
aChild = [[Child alloc] init];
}
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
{
currentElementValue = [[NSMutableString alloc] initWithString:string];
}
else
{
[currentElementValue appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//NSLog(@"El name: %@", elementName);
if([elementName isEqualToString:@"GetKidsResult"])
NSLog(@"end of xml");
return;
if([elementName isEqualToString:@"a:KeyValueOfintKidf4KEWLbb"])
{
NSLog(@"Found end of child");
[appDelegate.children addObject:aChild];
aChild = nil;
}
else if([elementName isEqualToString:@"a:Key"])
{
NSLog(@"Found key");
}
else if([elementName isEqualToString:@"b:CheckedIn"])
{
NSLog(@"Found checkedIn");
}
else if([elementName isEqualToString:@"b:FirstName"])
{
NSLog(@"Found firstname");
}
else if([elementName isEqualToString:@"b:Gender"])
{
NSLog(@"found gender");
}
else if([elementName isEqualToString:@"b:Id"])
{
NSLog(@"found id");
}
else if([elementName isEqualToString:@"b:IsOnTour"])
{
NSLog(@"found isontour");
}
else if([elementName isEqualToString:@"b:LastName"])
{
NSLog(@"found lastname");
}
else if([elementName isEqualToString:@"b:GroupName"])
{
NSLog(@"found groupname");
}
currentElementValue = nil;
}
@end
Right now I'm just trying to see if it finds the data... then I will save it afterwards... But in my didEndElement, it doesn't seem to find anything other than
if([elementName isEqualToString:@"GetKidsResult"])
An example of my xml response:
<a:KeyValueOfintKidf4KEWLbb>
<a:Key>XXXXXXXX</a:Key>
<a:Value xmlns:b="http://schemas.datacontract.org/2004/07/DayCare.DB.DTO">
<b:CheckedIn>false</b:CheckedIn>
<b:FirstName>XXXXXXX</b:FirstName>
<b:Gender>Male</b:Gender>
<b:Id>XXXXXXXX</b:Id>
<b:IsOnTour>false</b:IsOnTour>
<b:LastName>XXXXXX</b:LastName>
<b:Photo>XXXXXXXX</b:Photo>
<b:GroupName>XXXXX</b:GroupName>
</a:Value>
</a:KeyValueOfintKidf4KEWLbb>
Any idea as to why it doesn't the other elements? I've never done web services and such before, so the more simple the explanation, the better...
When I NSLog the element, I can see that it goes through the xml document properly
NSLog(@"Processing Element: %@", elementName);
Example:
Processing Element: a:KeyValueOfintKidf4KEWLbb
Processing Element: a:Key
Processing Element: a:Value
Processing Element: b:CheckedIn
Processing Element: b:FirstName
Processing Element: b:Gender
Processing Element: b:Id
Processing Element: b:IsOnTour
Processing Element: b:LastName
Processing Element: b:Photo
Processing Element: b:GroupName
Upvotes: 2
Views: 504
Reputation:
In didEndElement
, try changing this part:
if([elementName isEqualToString:@"GetKidsResult"])
NSLog(@"end of xml");
return;
to:
if([elementName isEqualToString:@"GetKidsResult"])
{
NSLog(@"end of xml");
return;
}
Without putting those two statements after the if
in a block, the return
gets executed every time.
Upvotes: 3