, parse the information find the weather condition which is in the element "yweather" under "code" (which is a number that means a diff. weather cond.)
Based on that I want to create a conditional statement that would be something like
//makes the cloud image appear on my view controller
if (code = 34){
[cloudy setHidden: NO];
}
Obviously, since I am posting in this site, it's not working. I challenge y'all to figure this out cause this is killing me. Before you think I'm creating another Weather app, let me assure you that this will only be a small feature on my otherwise complete app.
This is the code in my ViewController.m
Code:
- (void)openXMLFile{
NSString * path = @"http://weather.yahooapis.com/forecastrss?w=12791977";
[self parseXMLFile:path];
}
- (void)parseXMLFile:(NSString *)path
{
BOOL success;
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:path];
if (rssParser)
[rssParser release];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldResolveExternalEntities:YES];
success = [rssParser parse];
//[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
if ([elementName isEqualToString:@"yweather"]) {
NSString* title = [attributeDict valueForKey:@"text"];
int id = [[attributeDict valueForKey:@"code"] intValue];
NSLog(@"Title: %@, ID: %i", title, id);
NSLog(@"This is the NS LOG 3");
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
I may use PHP as too much of a crutch, but you could use a PHP scraper to grab the item you need, and then instead of accessing the page you indicated directly and trying to parse it out, just hit your PHP for the data directly...
For the other NSLog call, you may want to use %i or %d instead of %@ since you want to log an integer, not an object. I am new to Obj-C and everything iPhone related so I don't really know whether that is the reason for your (null).
Code:
NSLog(@"condition is: %i", code);
Cheers,
Bob
__________________ We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
...It's amazing how php did in 4-5 lines, what takes objective C a whole lot more.
You're comparing apples and oranges and clearly don't understand what the PHP does versus what the Obj-C code does. That PHP code does "in 4-5 lines" the very specific task of finding a particular sequence of letters and returning a fixed number of characters that follow. So if your XML looks like this:
Code:
<yweather:condition ... code="34" ... />
you should be OK. But if the XML ever looks like any of the following, your magical PHP code will suddenly be returning strange values:
Code:
<yweather:condition ... code="6" ... />
or
<yweather:condition ... code="125" ... />
or
<yweather:condition ... code= "34" ... />
or
<yweather:condition ... code ="34" ... />
Kalimba is right, using this as an intermediate is a brittle system...this will give you a bit more latitude if there is a minor syntax change (but still assumes no blank spaces within the quotes):