NSXML解析 - 我应该在这做什么? [代码示例]

时间:2011-09-19 06:54:11

标签: xml ios nsxmlparser

XML源

我想将当前的条件解析成字符串,例如"条件,在Celcius的温度,湿度"并将其显示在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

0 个答案:

没有答案