how to limit parsing in iphone?
I am new to iphone development .I want parse an image url from a xml file and display it in a RSS feed.There are three image url but i want to retrieve only one url and display it.
<entry>
<id>xxxxx</id>
<title>xxx xx开发者_如何学编程xx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://tiger.jpg"/>
<media:thumbnail url="http://lion.jpg"/>
<media:thumbnail url="http://elephan.jpg"/>
</media:group>
</entry>
<entry>
<id>xxxxx</id>
<title>xxx xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://parrot.jpg"/>
<media:thumbnail url="http://peacock.jpg"/>
<media:thumbnail url="http://sparrow.jpg"/>
</media:group>
</entry>
for parsing it
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"entry"]) {
entry = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
NSLog(@"inside image1 ");
}else if([elementName isEqualToString:@"media:thumbnail"])
{
if(myUrl==nil){
NSString* myUrl = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
}
}
}.
I want to retrieve only tiger and parrot image.But i get tiger twice.Please help me out.Thanks.
Simply keep a flag in your parser delegate that you reset when you see <media:group>
. Then every time you see a <media:thumbnail>
you get. Then every time you get a <media:thumbnail>
, check if the flag has been set. If not then this is the first one, so you take the data and set the flag. When you see the next <media:thumbnail>
you ignore it because the flag has been set.
In your case, myUrl
is the flag. So simply reset it to nil every tie you see the <media:group>
.
When you start parsing your entry
element reset myUrl
variable to nil - so you will parse "media:thumbnail" once for each 'entry' element
精彩评论