How to parse Google weather API using NSXML?

后端 未结 1 1650
春和景丽
春和景丽 2020-12-22 11:05

i want to parse google weather API using NSXML so please give me some Guidance for this.

This is My url

and i have taken such kind of steps:

         


        
相关标签:
1条回答
  • 2020-12-22 11:45

    hey ankit you can get this code if at all its helpful to you no need to establish connection

    just use this method

    -(id)initWithURL:(NSURL*)url arrayRootObjectTags:(NSArray*)arrTags sel:(SEL)seletor andHandler:(NSObject*)handler{
        if(self = [super init] ){
            self.mainArray=arrTags;
            self.MainHandler=handler;
            self.targetSelector=seletor;
            NSLog(@"%@",[url description]);
            NSURLRequest *req=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:30];                 
            con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
            if(con){
                myWebData=[[NSMutableData data] retain];
            } else {
                [MainHandler performSelector:@selector(targetSelector:) withObject:nil];
            }
        }
        return self;
    }
    

    also the other supporting method

    -(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict {
        if([elementName isEqualToString:@"html"] || [elementName isEqualToString:@"HTML"]){
            didGetHTML=YES; [self parserDidEndDocument:parser];
        } else if([[mainArray objectAtIndex:0] isEqualToString:elementName] && [[mainArray objectAtIndex:1] isEqualToString:elementName] && !didGetHTML) {
            objectsArray=[[NSMutableArray alloc] init];
            tmpD=[[NSMutableDictionary alloc] init];
            if(tmpOther==nil) tmpOther=[[NSMutableDictionary alloc] init];      
        } else if([[mainArray objectAtIndex:0] isEqualToString:elementName] && !didGetHTML ) {
            objectsArray=[[NSMutableArray alloc] init];
            if(tmpOther==nil) tmpOther=[[NSMutableDictionary alloc] init];      
        } else if([[mainArray objectAtIndex:1] isEqualToString:elementName] && !didGetHTML ) {
            tmpD=[[NSMutableDictionary alloc] init];
        } else if([mainArray containsObject:elementName] && !didGetHTML){
            [tmpD setValue:[attributeDict valueForKey:@"data"] forKey:elementName];
        }
    }
    

    -(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string { if(tmpString==nil && !didGetHTML){ tmpString=[[NSString alloc] initWithString:string]; } else if(!didGetHTML){ NSString *t=[NSString stringWithString:tmpString]; if([tmpString retainCount]>0) { [tmpString release]; tmpString=nil; } tmpString=[[NSString alloc] initWithFormat:@"%@%@",t,string]; } }

    -(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName {
        if([[mainArray objectAtIndex:0] isEqualToString:elementName] && [[mainArray objectAtIndex:1] isEqualToString:elementName] && !didGetHTML){
            [objectsArray addObject:tmpD];
        } else if([elementName isEqualToString:[mainArray objectAtIndex:1]] && !didGetHTML){
            [objectsArray addObject:tmpD];
            [tmpD release]; tmpD=nil;
        } else if([mainArray containsObject:elementName] && !didGetHTML) {
            if(![tmpD valueForKey:elementName]){
                [tmpD setValue:tmpString forKey:elementName];
            }
            [tmpString release]; tmpString=nil;     
        } else {
            [tmpOther setValue:tmpString forKey:elementName];
            [tmpString release]; tmpString=nil;
        }
    }
    

    and simply call the initwith url method from which ever class you have written this method

    just you have to give root tag object tag and element tag of a particular object tag and also give selector after that take the response in dictionary and they take it in array and display the result according to your value for key

    0 讨论(0)
提交回复
热议问题