views:

422

answers:

3

I am new to iphone development.I want parse an you-tube XML page and retrieve its contents and display in a RSS feed.

my xml page is

<entry>
<id>xxxxx</id>
<title>xxx xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
 <media:thumbnail url="http://tiger.jpg"/&gt;
</media:group>
</entry>

To retrieve the content i am using xml parsing.

- (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];
    currentcontent = [[NSMutableString alloc] init];
    }

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
if ([elementName isEqualToString:@"entry"]) {
    [entry setObject:currentTitle forKey:@"title"];
    [entry setObject:currentDate forKey:@"content"];
    [stories addObject:[entry copy]];
    }}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

if ([currentElement isEqualToString:@"title"]) {
    [currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"content"]) {
    [currentLink appendString:string];
 }
 }

I am able to retrieve id , title and content value and display it in a table-view.How can i retrieve tiger image URL and display it in table-view.Please help me out.Thanks.

+2  A: 
   - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            

    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"media:thumbnail"])
    imageUrl=[attributeDict objectForKey:@"url"];
}

Since you already have an if statement in this method, use else if for this one

Nithin
What data type i should declare the imageUrl
Warrior
what should be given in "parse did end element" and "found characters" method
Warrior
imageUrl should be a string, also for getting the attribute value, you need not specify about it in the other methods.
Nithin
where did it check for the element<media:group>
Warrior
the parser will be checking the tags continuously one after the other, no matter whether they are child nodes or parent nodes, but considers as a string. The values of the attributes will be stored in the attributeDict with the attribute name as the key. Since the attributes are within the tag, they are obtained at didStartElement: method. But value of the tag(here xxxxx for <id>) is obtained at foundCharacters: method.
Nithin
Thanks, i got it right........
Warrior
A: 

Well recently i tried to load 30- 50 data at a time from a huge xml file in mobile and then when user want to go to other page then it will load another 30-50. i mean pagination. i think i am not doing it in porper manner..can some one help me out.

mobile
You can post a new question regarding this from your stack-over flow account to get fruitful answer.
Warrior
A: 

I'm also trying to parse a URL from an XML file just like the above:

<media:content url="http://****.co.uk/wordpress/wp-content/uploads/2010/07/07-****.mp3" fileSize="1129523" type="audio/mpeg" />

My code has the two sections: didStartElement & didEndElement where the didEndElement is writing to the object:

[urlString setString:currentElementValue];

I've tried using objectForKey under didEndElement, but it doesn't declare the attributeDict?

Can anyone give me some advice as to how I should be doing this?

Thanks.

markturnip
use the above mentioned method - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
Rahul Vyas
Here's my current element delegate:if ([elementName isEqual:@"media:content"] linkString =[[NSMutableString alloc] init]; }Should I be adding linkString = [attributeDict objectForKey:@"url"]; below my linkString alloc?
markturnip