I’m currently making my first iPhone app for work, it’s designed to be an enhanced version of the leaflet given out to patients when they first arrive at the hospital. My idea was to take the information within and make it into something more iPhone friendly. On my journey through the magical world of iPhone development I’ve encountered a lot of issues as I’m more a web developer than an Objective-C developer. One of these issues which I can gladly say I’ve worked out was using a XML document with multiple child nodes to create an array of the data within these nodes.

My XML parsing script comes from Jason Terhorst’s excellent NSXMLParser RSS feed tutorial, and I used the solution to creating the array from this stackoverflow post. With this working fine I suddenly noticed that when running NSLog(@"%@",item); to see how the array was being generated that It was returning something like this:


{
"phone_number" = (
"020 8725 0001\n",
" \n",
" \n",
"020 0002 0001\n",
" \n",
" \n",
" \n"
);
title = "Accident and Emergency\n";
}

NSXMLParser was parsing the indents and line breaks that I put in my XML document so I could read it easily when writing it. I’ve heard that NSXMLParser does this if there’s no DOCTYPE defined in the XML but I couldn’t get my head around the syntax for that. I then found this post which although removed the whitespace and line breaks still returned blank array entries for the line breaks, this led me to the solution I used.

My solution (included in foundCharacters so you can see what I’ve done):

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:trimmedString];
}
if([currentElement isEqualToString:@"phone_number"]){
if([trimmedString isEqualToString:@""]){
return;
}else{
[phoneNumbers addObject:trimmedString];
}
}
}

By returning when the string is nothing it doesn’t create blank array entries.