xml attribute parsing using NSXMLParser

I am parsing the feed:

<item>
<title><![CDATA[Why bring a child into this world? Here&#039;s one reason]]></title>
<guid>http://www.ivillage.co.uk/why-bring-a-child-world-heres-one-reason/169019</guid>
<pubDate>2013-11-21T17:11:00</pubDate>
<type>article</type>
<link>http://www.ivillage.co.uk/why-bring-a-child-world-heres-one-reason/169019</link>
<description><![CDATA[Unilever advert asks: Why bring a child into this world? This viral video will make you cry as you realise that new babi...]]></description>
<media:thumbnail url="http://www.ivillage.co.uk/sites/dev.ivillage.co.uk/files/Unilever-636.jpg" width="636" height="477"/>    
<media:thumbnail url="http://www.ivillage.co.uk/sites/dev.ivillage.co.uk/files/Unilever-636.jpg" width="120" height="90"/>
<dc:creator>Ali Gray</dc:creator>
</item>

I want to get the thumbnail url in the parsers delegate:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName,

How to to that ?Thanks

Answers


The thumb url is the attribute. so you can do as follows:

Add NSString url; to your instance variable, and add the if statement in the following method:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"media:thumbnail"]) {
        url = attributeDict[@"url"];
    } 
}

import <NSXMLParserDelegate> in your .h file

in you .m file implement these 3 methods

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //create a mutable string & currentNodeContent is that mutable string
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"item"])
    {
        currentTweet = [Tweet alloc];
        isStatus = YES;
    }
    if ([elementname isEqualToString:@"dc:creator"])
    {
        isStatus = NO;
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (isStatus) 
    {
        if ([elementname isEqualToString:@"media:thumbnail"])
        {
            //Here use array to store both of you thumbnail items. I just add my example code to here, dateCreated is a string to store.
            currentTweet.dateCreated = currentNodeContent;
        }
    }
    if ([elementname isEqualToString:@"item"]) 
    {
        [self.tweets addObject:currentTweet];
        currentTweet = nil;
        currentNodeContent = nil;
    }
}

Need Your Help

Solution for image gallery

javascript html css html5 css3

Ok, to start off, I'm sure out in the internet there is a good tutorial about this, but I can't even manage to "write down" a possible title for the problem, and I apologise for that.

iOS & SpriteKit - low fps after restarting the game

ios iphone sprite-kit skscene

I have created a simple iPhone game where user is redirected to another view after loosing the game and can clik "Start again" to play it again.