随笔-XML数据解析-就是这几步

#import "XMLParser.h"

@implementation TUSprojectAppDelegate_iPhone

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self requestpushad];
}
-(void)requestpushad{
    pushdata=[[NSMutableData alloc] init];
    NSURL *url=[NSURL URLWithString:@"http://s3.amazonaws.com/imobile20110526/BrainwaveTuner2015/tunerpushAd.xml"];
    ASIHTTPRequest * myRequest = [ASIHTTPRequest requestWithURL:url];
    [myRequest setDelegate:self];
    [myRequest startAsynchronous];
}
- (NSString *)dataFilePath:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:name];
}
#pragma mark -
#pragma mark Download support (ASIHTTPRequestDelegate)
//断点下载开始
- (void)requestStarted:(ASIHTTPRequest *)request
{
    
}
//服务器有响应了
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary*)responseHeaders
{
    [self.pushdata setLength:0];
}
- (void)request:(ASIHTTPRequest*)request didReceiveData:(NSData *)data
{
    [self.pushdata appendData:data];
}
//下载完成
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSArray *languageArray = [NSLocale preferredLanguages];
    NSString *language = [languageArray objectAtIndex:0];
    NSString *downloadPath = [self dataFilePath:@"tunnerpushAd.xml"];
    [self.pushdata writeToFile:downloadPath atomically:YES];
    NSURL *xURL = [NSURL fileURLWithPath:downloadPath isDirectory:NO];
    NSData *musciXmlData = [NSData dataWithContentsOfURL:xURL];
    XMLParser *musciXmlparser = [[XMLParser alloc] initWithData:musciXmlData];
    [musciXmlparser setDelegate:musciXmlparser];
    [musciXmlparser parse];
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:musciXmlparser.array];
    NSMutableDictionary *dicAD=[NSMutableDictionary dictionaryWithDictionary:[array objectAtIndex:0]];
//    NSLog(@"dicAD=======%@",dicAD);
    if (array!=nil&&dicAD!=nil) {
        NSString *timeString=[[NSUserDefaults standardUserDefaults]objectForKey:@"pushupdate"];
        
        if (![timeString isEqualToString:[dicAD objectForKey:@"pushupdate"]]) {
        
            if([[dicAD objectForKey:@"isshow"] isEqualToString:@"on"]){
                
                NSString *title=[NSString stringWithFormat:@"title_%@",language];
                NSString *desc=[NSString stringWithFormat:@"description_%@",language];
               
                NSMutableDictionary *adDic=[NSMutableDictionary dictionaryWithCapacity:0];
                [adDic setObject:[dicAD objectForKey:title] forKey:@"title"];
                [adDic setObject:[dicAD objectForKey:desc] forKey:@"description"];
                [adDic setObject:[dicAD objectForKey:@"applink"] forKey:@"applink"];
                [adDic setObject:[dicAD objectForKey:@"pushupdate"] forKey:@"pushupdate"];
//                [adDic setObject:[dicAD objectForKey:@"isshow"] forKey:@"isshow"];
                
                
                logodata=[[NSMutableData alloc] init];
                NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[dicAD objectForKey:@"logoImage"]]] delegate:self];
               
                [[NSUserDefaults standardUserDefaults] setObject:adDic forKey:@"AdInfoDicc"];
                [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"showAD"];
            }else
            {
                [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"showAD"];
            }
        }
        [self.pushdata setLength:0];
    }
}
//下载出错
- (void)requestFailed:(ASIHTTPRequest *)request
{
    [self.pushdata setLength:0];
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.logodata appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    self.logodata = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    
   // NSString *logoFileName = [self dataFilePath:@"pushAdlogo.plist"];
   // NSDictionary *logoDic = [NSDictionary dictionaryWithObject:self.logodata forKey:@"pushlogo"];
   // [logoDic writeToFile:logoFileName atomically:YES];
    [[NSUserDefaults standardUserDefaults]setObject:self.logodata forKey:@"logoDic"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self.logodata setLength:0];
}
@end

@implementation ViewControlleriPhone

-(void)viewWillAppear:(BOOL)animated
{
    NSMutableDictionary *adDicc = [NSMutableDictionary dictionaryWithCapacity:0];
    adDicc = [[NSUserDefaults standardUserDefaults]objectForKey:@"AdInfoDicc"];
   
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"showAD"]isEqualToString:@"YES"]){
        self.bombImageView.image=[UIImage imageWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"logoDic"]];
        NSLog(@"logoimage = %@",[[NSUserDefaults standardUserDefaults]objectForKey:@"logoDic"]);
        self.bombLabel.text = [adDicc objectForKey:@"title"];
        self.bombTextView.text = [adDicc objectForKey:@"description"];
        
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"begin"]isEqualToString:@"yes"]) {
            self.bombView.hidden = NO;
            [self.view bringSubviewToFront:self.bombView];
        }else
        {
            self.bombView.hidden = YES;
        }
    }else
    {
        self.bombView.hidden = YES;
    }
}
@end


你可能感兴趣的:(ios,xml,数据解析)