Reputation: 931
heightI'm a noob to iphone development and I am trying to parse an xml with elements that contain multiple attributes. I have several tutorials on parsing XML, but none of them show the steps to parse an element with multiple attributes. I am trying to parse the xml with NSXMLParser. I want to parse the first occurrence of the media:thumbnail element for the value of the second attribute. Any help is greatly appreciated.
MY CODE:
//XML
<item>
<title>March 15 AM Metals Commentary: Eric Zuccarelli</title>
<link>http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2228319176001?src=mrss</link>
<description>Eric Zuccarelli, Independent Copper Trader</description>
<guid>http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2228319176001?src=mrss</guid>
<pubDate>Fri, 15 Mar 2013 06:40:38 -0700</pubDate>
<media:player height="546" url="http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2228319176001?src=mrss" width="966" / >
<media:keywords>commentary,CME Group,financial products,cmedaily,nymex,metals,youtube,Market Commentary,Zuccarelli</media:keywords>
<media:thumbnail height="90" url="http://brightcove.vo.llnwd.net/d21/unsecured/media/49919183001/49919183001_2228333209001_th-514324d6e4b02e906f7476ba-806787304001.jpg?pubId=49919183001" width="120" / >
<media:thumbnail height="360" url="http://brightcove.vo.llnwd.net/d21/unsecured/media/49919183001/49919183001_2228333208001_vs-514324d6e4b02e906f7476ba-806787304001.jpg?pubId=49919183001" width="480" / >
<media:category>Metals</media:category>
<bc:playerid>1683318714001</bc:playerid>
<bc:lineupid>1644543007001</bc:lineupid>
<bc:titleid>2228319176001</bc:titleid>
<bc:duration>215</bc:duration>
<dcterms:valid / >
<bc:accountid>49919183001</bc:accountid>
</item>
My code is as follws:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentBright = [[NSMutableString alloc] init];
if([elementName isEqualToString:@"media:thumbnail"]){
if([attributeDict objectForKey:@"height"]==90){
currentBright = [attributeDict objectForKey:@"url"];
NSLog(@"Brightcove Url:%@", currentBright); //<--Not reaching this point.
}
}
}
}
EDIT
if([elementName isEqualToString:@"media:thumbnail"]&&[attributeDict objectForKey:@"height"]==90){
currentBright = [attributeDict objectForKey:@"
NSLog(@"Brightcove Url:%@", currentBright);
}
Upvotes: 0
Views: 362
Reputation: 1858
Just tryout this....
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentBright = [[NSMutableString alloc] init];
}
else if([elementName isEqualToString:@"media:thumbnail"]) {
if([attributeDict objectForKey:@"height"]==90) {
currentBright = [attributeDict objectForKey:@"url"];
NSLog(@"Brightcove Url:%@", currentBright);
}
}
}
Upvotes: 1
Reputation: 6229
You're never going to hit your second or third conditional because how can elementName have changed without being advanced? Instead you should un-nest your conditionals.
Right now what happens is:
-didStartTag
ElementName = item -> first condtional hit -> fails second
-didStartTag
ElementName = title -> fails first conditonal
etc....
Upvotes: 0