Re: iPhone: NSXMLParser problems
Re: iPhone: NSXMLParser problems
- Subject: Re: iPhone: NSXMLParser problems
- From: Jeremy Pereira <email@hidden>
- Date: Wed, 6 Jan 2010 14:58:50 +0000
Trying again, the HTML markup in the previous version of this e-mail sent the size over the list limit. Apologies if the original also turns up....
I created a little test program to run the XML through a parser and mine worked which was a bit mystifying until I figured out what the problem is.
getWeatherXmlForZipCode: is a class method. You have set the delegate to the class not to an object of the class.
On 5 Jan 2010, at 15:58, Eric E. Dolecki wrote:
> http://weather.yahooapis.com/forecastrss?p=01701
>
> (For an example)
>
> Here is all the code in question:
>
> +(NSString*)getWeatherXmlForZipCode: (NSString*)zipCode {
^^^^ + means class method
<snip>
> NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
> [parser setDelegate:self];
^^^^ self in a class method is the class itself.
>
> - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
^^^^ - means not a class method.
I attach my little test program for completeness and the output. There's no error checking or anything that would make it production quality.
----
#import <Foundation/Foundation.h>
@interface MyParser : NSObject <NSXMLParserDelegate>
{
}
-(void) parse: (NSString*) urlString;
@end
@implementation MyParser
-(void) parse: (NSString*) urlString
{
NSURL* url = [[NSURL alloc] initWithString: urlString];
NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL: url];
[url release];
[parser setDelegate: self];
[parser setShouldProcessNamespaces: YES];
[parser setShouldReportNamespacePrefixes:YES];
[parser setShouldResolveExternalEntities:YES];
[parser parse];
[parser release];
}
-(void) parser: (NSXMLParser *) parser
didStartElement: (NSString*) elementName
namespaceURI: (NSString*) namespaceURI
qualifiedName: (NSString*) qualifiedName
attributes: (NSDictionary*) attributeDict
{
NSLog(@"element %@, ns %@, qn %@", elementName, namespaceURI, qualifiedName);
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyParser* parser = [[MyParser alloc] init];
[parser parse: @"http://weather.yahooapis.com/forecastrss?p=01701"];
[parser release];
[pool drain];
return 0;
}
----
2010-01-06 14:46:49.410 TestNSXMLParser[17401:a0f] element rss, ns , qn rss
2010-01-06 14:46:49.412 TestNSXMLParser[17401:a0f] element channel, ns , qn channel
2010-01-06 14:46:49.412 TestNSXMLParser[17401:a0f] element title, ns , qn title
2010-01-06 14:46:49.412 TestNSXMLParser[17401:a0f] element link, ns , qn link
2010-01-06 14:46:49.412 TestNSXMLParser[17401:a0f] element description, ns , qn description
2010-01-06 14:46:49.413 TestNSXMLParser[17401:a0f] element language, ns , qn language
2010-01-06 14:46:49.413 TestNSXMLParser[17401:a0f] element lastBuildDate, ns , qn lastBuildDate
2010-01-06 14:46:49.413 TestNSXMLParser[17401:a0f] element ttl, ns , qn ttl
2010-01-06 14:46:49.413 TestNSXMLParser[17401:a0f] element location, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:location
2010-01-06 14:46:49.414 TestNSXMLParser[17401:a0f] element units, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:units
2010-01-06 14:46:49.414 TestNSXMLParser[17401:a0f] element wind, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:wind
2010-01-06 14:46:49.414 TestNSXMLParser[17401:a0f] element atmosphere, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:atmosphere
2010-01-06 14:46:49.414 TestNSXMLParser[17401:a0f] element astronomy, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:astronomy
2010-01-06 14:46:49.414 TestNSXMLParser[17401:a0f] element image, ns , qn image
2010-01-06 14:46:49.415 TestNSXMLParser[17401:a0f] element title, ns , qn title
2010-01-06 14:46:49.415 TestNSXMLParser[17401:a0f] element width, ns , qn width
2010-01-06 14:46:49.415 TestNSXMLParser[17401:a0f] element height, ns , qn height
2010-01-06 14:46:49.415 TestNSXMLParser[17401:a0f] element link, ns , qn link
2010-01-06 14:46:49.415 TestNSXMLParser[17401:a0f] element url, ns , qn url
2010-01-06 14:46:49.415 TestNSXMLParser[17401:a0f] element item, ns , qn item
2010-01-06 14:46:49.416 TestNSXMLParser[17401:a0f] element title, ns , qn title
2010-01-06 14:46:49.416 TestNSXMLParser[17401:a0f] element lat, ns http://www.w3.org/2003/01/geo/wgs84_pos#, qn geo:lat
2010-01-06 14:46:49.416 TestNSXMLParser[17401:a0f] element long, ns http://www.w3.org/2003/01/geo/wgs84_pos#, qn geo:long
2010-01-06 14:46:49.417 TestNSXMLParser[17401:a0f] element link, ns , qn link
2010-01-06 14:46:49.417 TestNSXMLParser[17401:a0f] element pubDate, ns , qn pubDate
2010-01-06 14:46:49.417 TestNSXMLParser[17401:a0f] element condition, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:condition
2010-01-06 14:46:49.417 TestNSXMLParser[17401:a0f] element description, ns , qn description
2010-01-06 14:46:49.418 TestNSXMLParser[17401:a0f] element forecast, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:forecast
2010-01-06 14:46:49.418 TestNSXMLParser[17401:a0f] element forecast, ns http://xml.weather.yahoo.com/ns/rss/1.0, qn yweather:forecast
2010-01-06 14:46:49.418 TestNSXMLParser[17401:a0f] element guid, ns , qn guid
----_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden