Bogdan Vitoc
Bogdan Vitoc

Reputation: 139

How to use NSXMLParser to Find 1 Element

I have searched the internet for several days, but I cannot find a consise answer. I want to make a simple practice weather app that shows the temperature for a hardcoded zip code.

Here is the XML

<data>
<request>
<type>Zipcode</type>
<query>08003</query>
</request>
<current_condition>
<observation_time>08:29 PM</observation_time>
<temp_C>11</temp_C>
<temp_F>52</temp_F>
<weatherCode>143</weatherCode>
<weatherIconUrl>
<![CDATA[
http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0006_mist.png
]]>
</weatherIconUrl>
<weatherDesc>
<![CDATA[ Mist ]]>
</weatherDesc>
<windspeedMiles>4</windspeedMiles>
<windspeedKmph>7</windspeedKmph>
<winddirDegree>210</winddirDegree>
<winddir16Point>SSW</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>87</humidity>
<visibility>5</visibility>
<pressure>1013</pressure>
<cloudcover>100</cloudcover>
</current_condition>
<weather>
<date>2012-12-08</date>
<tempMaxC>13</tempMaxC>
<tempMaxF>55</tempMaxF>
<tempMinC>9</tempMinC>
<tempMinF>48</tempMinF>
<windspeedMiles>6</windspeedMiles>
<windspeedKmph>9</windspeedKmph>
<winddirection>W</winddirection>
<winddir16Point>W</winddir16Point>
<winddirDegree>260</winddirDegree>
<weatherCode>122</weatherCode>
<weatherIconUrl>
<![CDATA[
http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png
]]>
</weatherIconUrl>
<weatherDesc>
<![CDATA[ Overcast ]]>
</weatherDesc>
<precipMM>3.1</precipMM>
</weather>
</data>

ALL I want to do is to extract the *temp_F* and store it in a NSString.

Upvotes: 1

Views: 426

Answers (2)

Miles Alden
Miles Alden

Reputation: 1540

Since you already mentioned using NSXMLParser, just go with that. Set your delegate to implement the protocol

 @interface MyClass : NSObject <NSXMLParserDelegate>

Watch for the opening tag of your xml entry (looks to be in this case) with something like

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

      if ( [elementName isEqualToString:@"temp_F"] ) {
           // Set flag and reset string
           self.foundTargetElement = true;
           if ( self.myMutableString ) {
                self.myMutableString = nil;
                self.myMutableString = [[NSMutableString alloc] init];
           }
      }
 }

Next implement

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
       if ( self.foundTargetElement ) {
             [self.myMutableString appendString:string];
       }     
 }

and using the same pattern as above, watch for your tag, () and append its value to your string, or do whatever else you want with the data:

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

      self.foundTargetElement = false;

      // Do something with your result, or
      // Wait until entire document has been parsed.
 }

Let me know if that works out for you.

Upvotes: 1

rmaddy
rmaddy

Reputation: 318784

If all you want is a single value for a single element that only appears once in the XML then I would do some simple string search instead of bothering with a full blown XML parser.

Get the range of the substring @"<temp_F>" and the substring @"</temp_F>" and grab the value in between.

Upvotes: 2

Related Questions