Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSXMLParser Parsing Attributes

How do you extract attributes from XML using NSXML parser??

Heres my xml:

<item>
<title>Button hails 'amazing' win</title>
<description>Jenson Button hailed yesterday's crazy Canadian Grand Prix victory as the best of his Formula One career.
</description>
<link>http://www.skysports.com/story/0,19528,12433_6986809,00.html</link>
<guid isPermaLink="false">12433_6986809</guid>
<pubDate>Mon, 13 Jun 2011 06:21:00 GMT</pubDate>
<category>News Story</category>
<enclosure type="image/jpg" url="http://img.skysports.com/11/06/128x67/Canadian-GP-Jenson-Button-celebrates1_2609288.jpg" length="123456" />
</item>

I need to get the url from the enclosure tags.

Thanks

like image 857
MrPink Avatar asked Jun 13 '11 10:06

MrPink


Video Answer


3 Answers

if([elementName isEqualToString:@"enclosure"])
{
    NSString *urlValue=[attributeDict valueForKey:@"url"];
    NSString *urlValue=[attributeDict valueForKey:@"type"];
    NSString *urlValue=[attributeDict valueForKey:@"length"];
}
like image 113
Gypsa Avatar answered Oct 05 '22 15:10

Gypsa


you need to use NSXMLParser and its delegate functions

-

 (BOOL) parse:(NSData *)xmlData 


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



}

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

}

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

}

then you need to use some thing like this

if([elementName isEqualToString:@"enclosure"])
    {
        NSMutableDictionary *Dict=[NSMutableDictionary dictionary];
        [Dict setObject:[attributeDict valueForKey:@"url"] forKey:@"url"];
        [categoryDict setObject:[attributeDict valueForKey:@"type"] forKey:@"type"];

    }
like image 28
Ishu Avatar answered Oct 05 '22 17:10

Ishu


The method...

(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict

Gives you a dictionary of attributes and their keys (attributeDict)... look for an entry keyed "url" when the elementName is equal to "enclosure"...

like image 36
Simon Lee Avatar answered Oct 05 '22 16:10

Simon Lee