NSXML 解析 - 我应该在这里做什么? [代码示例]
XML 源
我想将当前条件解析为“条件、摄氏温度、湿度”等字符串,并将其显示在 iPhone 的屏幕上。
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>...</forecast_information>
<current_conditions>
<condition data="Overcast"/>
<temp_f data="61"/>
<temp_c data="16"/>
<humidity data="Humidity: 83%"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<wind_condition data="Wind: N at 0 mph"/>
</current_conditions>
<forecast_conditions>...</forecast_conditions>
<forecast_conditions>...</forecast_conditions>
<forecast_conditions>...</forecast_conditions>
<forecast_conditions>...</forecast_conditions>
</weather>
</xml_api_reply>
我的代码
1.ParserViewController.h
#import <UIKit/UIKit.h>
@interface ParserViewController : UIViewController <NSXMLParserDelegate> {
IBOutlet UITextField *cityName;
IBOutlet UILabel *details;
NSString *condition,*temp_c,*humidity;
NSXMLParser *xmlParser;
NSData *xmlData;
NSMutableString *xmlResults;
BOOL elementFound;
NSMutableArray *dataNames;
}
@property(nonatomic,retain) IBOutlet UITextField* cityName;
@property(nonatomic,retain) IBOutlet UILabel* details;
@property(nonatomic,retain) NSXMLParser *parser;
@property(nonatomic,retain) NSString *condition,*temp_c,*humidity;
-(IBAction)startProcess;
-(void) parseData:(NSData*)data;
@end
2.ParserViewController.m
#import "ParserViewController.h"
@implementation ParserViewController
@synthesize cityName,details;
@synthesize condition,temp_c,humidity;
@synthesize parser;
-(IBAction) startProcess
{
[cityName resignFirstResponder];
NSString *xmlString =
[NSString stringWithFormat:@"http://google.com/ig/apiweather=%@",cityName.text];
NSURL *xmlURL = [NSURL URLWithString:xmlString];
NSString *fileContents=[NSString stringWithContentsOfURL:xmlURL];
NSData *fileData=[fileContents dataUsingEncoding:NSUTF8StringEncoding];
[self parseData:fileData];
}
-(void) parseData:(NSData*)data
{
if (xmlParser)
[xmlParser release];
xmlParser=[[NSXMLParser alloc]initWithData:data];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
- (void)parser:(NSXMLParser *)xmlParser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Inside didStartElement");
if ([elementName isEqualToString:@"current_conditions"])
{
NSLog(@"Began");
/* Is the following code correct?. Please point out the mistake*/
NSString *dataItem=[[NSMutableString alloc]initWithString:[attributeDict valueForKey:@"data"]];
[dataNames addObject:dataItem];
elementFound=YES;
}
}
-(void)parser:(NSXMLParser *)xmlParser foundCharacters:(NSString *)string {
NSLog(@"Inside foundCharacters");
if(elementFound) {
/*What should I do here? Please help*/
}
}
-(void)parser:(NSXMLParser *)xmlParser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"Inside didEndElement");
if ([elementName isEqualToString:@"current_conditions"])
/*What should I do here? Please help*/
}
- (void)viewDidLoad {[super viewDidLoad];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
- (void)viewDidUnload {}
- (void)dealloc {[super dealloc];}
@end
XML source
I want to parse out the current conditions into strings like "Condition, temparature in Celcius, humidity" and display it on a screen in iPhone.
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>...</forecast_information>
<current_conditions>
<condition data="Overcast"/>
<temp_f data="61"/>
<temp_c data="16"/>
<humidity data="Humidity: 83%"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<wind_condition data="Wind: N at 0 mph"/>
</current_conditions>
<forecast_conditions>...</forecast_conditions>
<forecast_conditions>...</forecast_conditions>
<forecast_conditions>...</forecast_conditions>
<forecast_conditions>...</forecast_conditions>
</weather>
</xml_api_reply>
My Code
1. ParserViewController.h
#import <UIKit/UIKit.h>
@interface ParserViewController : UIViewController <NSXMLParserDelegate> {
IBOutlet UITextField *cityName;
IBOutlet UILabel *details;
NSString *condition,*temp_c,*humidity;
NSXMLParser *xmlParser;
NSData *xmlData;
NSMutableString *xmlResults;
BOOL elementFound;
NSMutableArray *dataNames;
}
@property(nonatomic,retain) IBOutlet UITextField* cityName;
@property(nonatomic,retain) IBOutlet UILabel* details;
@property(nonatomic,retain) NSXMLParser *parser;
@property(nonatomic,retain) NSString *condition,*temp_c,*humidity;
-(IBAction)startProcess;
-(void) parseData:(NSData*)data;
@end
2.ParserViewController.m
#import "ParserViewController.h"
@implementation ParserViewController
@synthesize cityName,details;
@synthesize condition,temp_c,humidity;
@synthesize parser;
-(IBAction) startProcess
{
[cityName resignFirstResponder];
NSString *xmlString =
[NSString stringWithFormat:@"http://google.com/ig/apiweather=%@",cityName.text];
NSURL *xmlURL = [NSURL URLWithString:xmlString];
NSString *fileContents=[NSString stringWithContentsOfURL:xmlURL];
NSData *fileData=[fileContents dataUsingEncoding:NSUTF8StringEncoding];
[self parseData:fileData];
}
-(void) parseData:(NSData*)data
{
if (xmlParser)
[xmlParser release];
xmlParser=[[NSXMLParser alloc]initWithData:data];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
- (void)parser:(NSXMLParser *)xmlParser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Inside didStartElement");
if ([elementName isEqualToString:@"current_conditions"])
{
NSLog(@"Began");
/* Is the following code correct?. Please point out the mistake*/
NSString *dataItem=[[NSMutableString alloc]initWithString:[attributeDict valueForKey:@"data"]];
[dataNames addObject:dataItem];
elementFound=YES;
}
}
-(void)parser:(NSXMLParser *)xmlParser foundCharacters:(NSString *)string {
NSLog(@"Inside foundCharacters");
if(elementFound) {
/*What should I do here? Please help*/
}
}
-(void)parser:(NSXMLParser *)xmlParser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"Inside didEndElement");
if ([elementName isEqualToString:@"current_conditions"])
/*What should I do here? Please help*/
}
- (void)viewDidLoad {[super viewDidLoad];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
- (void)viewDidUnload {}
- (void)dealloc {[super dealloc];}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论