views:

96

answers:

2

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 xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://tiger.jpg"/&gt;
<media:thumbnail url="http://lion.jpg"/&gt;
<media:thumbnail url="http://elephan.jpg"/&gt;
</media:group>
</entry>
<entry>
<id>xxxxx</id>
<title>xxx xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://parrot.jpg"/&gt;
<media:thumbnail url="http://peacock.jpg"/&gt;
<media:thumbnail url="http://sparrow.jpg"/&gt;
</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.

+1  A: 

When you start parsing your entry element reset myUrl variable to nil - so you will parse "media:thumbnail" once for each 'entry' element

Vladimir
Thanks .It works fine..
Warrior
+2  A: 

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>.

St3fan
Thanks.It works fine..
Warrior