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

发布于 2024-12-05 09:38:35 字数 3759 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文