开发者

iPhone: Object is null in another view when set with values read from XML

开发者 https://www.devze.com 2023-03-26 22:46 出处:网络
I am reading an XML from and parsing the data in one viewController class. When I try to access the object in another view, it appears as null. The NSMutableArray is set with values in the earlier vie

I am reading an XML from and parsing the data in one viewController class. When I try to access the object in another view, it appears as null. The NSMutableArray is set with values in the earlier view. I need these values to set a picker view. Could you please let me know where am I going wrong? I have attached the code below: The value of testArray is null in the other view.

//Disclaimer.m file

NSString *soapMessage = [NSString开发者_JAVA百科 stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                             "<soap:Body>\n"
                             "<GetCommonCodeByCatg xmlns=\"http://longbeach.gov/PDPropertyReg/Services/\">\n"
                             "<catg>COLOR</catg>\n"
                             "</GetCommonCodeByCatg>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n"];
    NSLog(@"Soap Message%@",soapMessage);

    NSURL *url =[NSURL URLWithString:@"http://wwwbitdemo.longbeach.gov/PDPropertyReg/Services/CommonCode.asmx"];
    NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:url];
    NSString *msgLength =[NSString stringWithFormat:@"%d",[soapMessage length]];

    [theRequest addValue:@"text/xml; charset =utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"http://longbeach.gov/PDPropertyReg/Services/GetCommonCodeByCatg" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        webData = [[NSMutableData data]retain];
    }
    else {
        NSLog(@"The connection is null");
    }


}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [webData setLength:0];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [webData appendData:data];



}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Connection Error");
    [connection release];
    [webData release];



}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@"Done, Received bytes: %d",[webData length]);

    NSString *theXML =[[NSString alloc] initWithBytes:[webData mutableBytes]length:[webData length]encoding:NSUTF8StringEncoding];
    NSLog(@"XML value %@",theXML);
    [theXML release];

    if (xmlParser) {
        [xmlParser release];
    }

    xmlParser = [[NSXMLParser alloc]initWithData:webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];


    [connection release];
    index =0;

}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{



    if ([elementName isEqualToString:@"GetCommonCodeByCatgResult"]) {
        self.testArray = [[NSMutableArray alloc]init];
    }

    else if ([elementName isEqualToString:@"SvcCommonCode"]) {
        aCategory =[[Category alloc]init];
        if ([elementName isEqualToString:@"CMCode"]) {
        aCategory.CMCode = [attributeDict objectForKey:@"CMCode"];
        }
        NSLog(@"Reading the CMCode: %@",aCategory.CMCode);
    }

    NSLog(@"Processing element: %@", elementName);
}

-(void)parser: (NSXMLParser *)parser foundCharacters:(NSString *)string{

    if (!currentElementValue) 
        currentElementValue =[[NSMutableString alloc]initWithString:string];
        else 
            [currentElementValue appendString:string];

        NSLog(@"Processing value:%@",currentElementValue);





}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if ([elementName isEqualToString:@"GetCommonCodeByCatgResult"]) {
        return;
    }
    if ([elementName isEqualToString:@"SvcCommonCode"]) {



        [self.testArray insertObject:aCategory atIndex:index];


        index =index+1;



        [aCategory release];
        aCategory =nil;
    }

    else 
        [aCategory setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
    NSLog(@"Item count %i",[self.testArray count]);


}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号