Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-28-2010, 09:00 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 24
dmelgoza is on a distinguished road
Arrow challenge

Okay,
so, I've spent days trying to connect to
Code:
http://weather.yahooapis.com/forecastrss?w=12791977
, 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{

	
}
dmelgoza is offline   Reply With Quote
Old 04-28-2010, 09:16 AM   #2 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

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...

something like:

PHP Code:
<?php
$url
="http://weather.yahooapis.com/forecastrss?w=12791977";
$file=file_get_contents($url);
$start=strpos($file,'code=');
$data=substr($file$start+62);
echo 
$data;
?>
and then:

PHP Code:
NSString *weatherURL = [ NSString stringWithFormat: @"http://thatfile.php"];
NSData *weatherData = [NSData dataWithContentsOfURL:[NSURL URLWithString:weatherURL]];
NSString *weatherString = [[NSString allocinitWithBytes: [weatherData byteslength:[weatherData lengthencodingNSUTF8StringEncoding];
int code;
code = [weatherString intValue]; 

Last edited by RobotWoods; 04-28-2010 at 09:19 AM.
RobotWoods is offline   Reply With Quote
Old 04-28-2010, 09:53 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 24
dmelgoza is on a distinguished road
Default php question

Okay, so I created the php file it returns 34 when viewed in the browser

but my conditional statement wasn't working right, so I tried NSlogging "code" and it came out "condition is: (null)" in the console.

Code:
NSString *weatherURL = [ NSString stringWithFormat: @"http://www.etzmedia.com/iphone/parse.php"];
	 NSData *weatherData = [NSData dataWithContentsOfURL:[NSURL URLWithString:weatherURL]];
	 NSString *weatherString = [[NSString alloc] initWithBytes: [weatherData bytes] length:[weatherData length] encoding: NSUTF8StringEncoding];
	 int code;
	 code = [weatherString intValue];  
	 NSLog(@"condition is: %@", code);
	 
	 if (code = 12){
		 //NSLog(@"code is 34");
	 }
	 else
	 {
		//NSLog(@"code is not 34"); 
	 //[cloudy setHidden:YES];
	 }
 }
something is not connecting right, any ideas?
dmelgoza is offline   Reply With Quote
Old 04-28-2010, 10:18 AM   #4 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

In your code it says,

Code:
	 if (code = 12){
		 //NSLog(@"code is 34");
	 }
you check for 12 but report 34. Is that intended?

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.

Consider saying thanks by buying my app. :]

Last edited by Robert Paulson; 04-28-2010 at 10:20 AM.
Robert Paulson is offline   Reply With Quote
Old 04-28-2010, 10:38 AM   #5 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: ¿La Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Looking at the XML returned from that URL, I'm seeing this as the element containing the 'code' attribute:
Code:
<yweather:condition  text="Fair"  code="34"  temp="62"  date="Wed, 28 Apr 2010 8:53 am CDT" />
so I believe you should be checking for an element named "yweather:condition", not just "yweather".

Your code to obtain the value of the "code" attribute looks fine, but the condition in your 'if' statement is wrong. You're doing this:
Code:
if ( code = 34 )
{
   blah blah blah...
}
where you should be doing this:
Code:
if ( code == 34 ) // '==' is the test for equality operator
{
   blah blah blah...
}
So, to wrap up, you probably want your code to look something like this:
Code:
if ( [elementName isEqualToString:@"yweather:condition"] )
{
	NSString * title = [attributeDict valueForKey:@"text"];    
	int id = [[attributeDict valueForKey:@"code"] intValue];
	NSLog(@"Title: %@, ID: %i", title, id);
}
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-28-2010, 11:38 AM   #6 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 24
dmelgoza is on a distinguished road
Default Totally worked!

guys it totally worked! not so thrilled about having to use php, BUT it worked so who cares???!!!!!

Haha!

It's amazing how php did in 4-5 lines, what takes objective C a whole lot more.
dmelgoza is offline   Reply With Quote
Old 04-28-2010, 01:22 PM   #7 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: ¿La Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by dmelgoza View Post
...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" ... />
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-28-2010, 01:39 PM   #8 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

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):

PHP Code:
<?php
$url
="http://weather.yahooapis.com/forecastrss?w=12791977";
$file=file_get_contents($url);
$item=strpos($file,'<yweather:condition');
$start=strpos($file,'code',$item);
$open=strpos($file,'\"',$start);
$close=strpos($file,'\"',$open+1);
$data=substr($file$open+1,$close-$open-1);
echo 
$data;
?>

Last edited by RobotWoods; 04-28-2010 at 07:13 PM.
RobotWoods is offline   Reply With Quote
Reply

Bookmarks

Tags
api, parse, weather, xml, yahoo

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 327
21 members and 306 guests
Abidullah, baja_yu, cgokey, Domele, Duncan C, Fstuff, gbenna, givensur, guusleijsten, HowEver, iphonedevshani, jbro, JoeRCruso, mdpauley, n00b, newDev, seokwon lee, SLIC, stanny, Steven.C, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,876
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:37 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0