I am working on an iOS app that uses NSURL to consume XML data from a website: http://undignified.podbean.com/feed. From there I parse through the XML using NSXMLParser and search for an attribute called “url” within the “enclosure” element. All attributes that match “url” are then added to a Mutable array labeled _links. The problem is every time a “url” attribute is found and is added to the array, it’s adding it into the previous entry and creating essentially one big object in the array. If I NSLog out the array, _links, the following is displayed:
(
(
"http://undignified.podbean.com/mf/feed/hm8a2d/UndignifiedBonusShow.mp3",
"http://undignified.podbean.com/mf/feed/cfdayc/UndignifiedShow03.mp3",
"http://undignified.podbean.com/mf/feed/wbbpjw/UndignifiedShow02Final.mp3",
"http://undignified.podbean.com/mf/feed/ih2x8r/UndignifiedShow01.mp3",
"http://undignified.podbean.com/mf/feed/t4x54d/UndignifiedShow00.mp3"
)
)
I’m using addObject for the array and have attempted insertObject, but neither have worked. My code is listed below. I know in other instances of loading objects into an array I had to implement a for loop, but am not sure if it is necessary here as the parser method appears to loop through the entire XML document by itself. I’m sure it’s something small I’m missing and a nudge in the right direction would be greatly appreciated.
UnDigParser.H – Hear file for parsing:
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> {
}
@property (retain) NSMutableArray *links;
@end
UnDigParser.M – Implementation file for parsing:
@implementation UnDigParser
NSMutableArray *_links;
@synthesize links = _links;
-(void)parserDidStartDocument:(NSXMLParser *)parser{
_links=[[NSMutableArray alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"enclosure"]){
NSString *link = [attributeDict objectForKey:@"url"];
if (link){
[_links addObject:link];
}
}
NSLog(@"%@",_links);
}
-(BOOL)parse{
self.delegate = self;
return [super parse];
}
@end
I believe you built an array within an array.
I would get rid of “NSMutableArray *_links;”
and change “[_links addObject:link];” to “[self.links addObject:link]”
and “_links = [[NSMutableArray alloc] init];” to “links = [[NSMutableArray alloc] init];”
Then just use self.links when you want to refer to the links. Don’t use all of the _links to refer to it.
How I would write it (a couple other things changed too):
UnDigParser.M – Implementation file for parsing: