1

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.

4

2 回答 2

2

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.

于 2012-12-08T21:59:20.357 回答
1

既然你已经提到过使用 NSXMLParser,那就继续吧。设置您的委托以实现协议

 @interface MyClass : NSObject <NSXMLParserDelegate>

注意你的 xml 条目的开始标签(在这种情况下看起来是这样)

 - (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];
           }
      }
 }

下一个实现

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

并使用与上述相同的模式,注意您的标签 () 并将其值附加到您的字符串,或者对数据执行任何其他操作:

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

让我知道这是否适合你。

于 2012-12-08T22:30:45.710 回答