iPhone:如何在同一视图中访问多个XML

时间:2011-08-18 22:13:56

标签: iphone

我正在尝试从两个不同的XML中读取数据,以填充我在iPhone中的View字段。

有没有办法在同一个视图中读取多个XML?我可以读取和解析单个XML。

由于

//使用NSXML Parser

-(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];
         [webData release];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser{
         NSLog(@"found file and started parsing");

         //colorTypes = [[NSMutableArray alloc]init];
         propertyCategories = [[NSMutableArray alloc]init];
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary
*)attributeDict{
         if ([elementName isEqualToString:@"GetCommonCodeByCatgResult"]) {
         }

         if ([elementName isEqualToString:@"SvcCommonCode"]) {

                 aCategory =[[Category alloc]init];

                 aCategory.CMCodeDesc = [attributeDict objectForKey:@"CMCodeDesc"];
                 }

}

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

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


}

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

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

    [propertyCategories addObject:aCategory.CMCodeDesc];
//[colorTypes addObject: aCategory.CMCodeDesc];
    [aCategory release];
        aCategory =nil;

 }

else
[aCategory setValue:currentElementValue forKey:elementName];
 [currentElementValue release];
currentElementValue = nil;

}

1 个答案:

答案 0 :(得分:1)

使用布尔值,将其命名为 isParsingNextString 来解析不同的字符串和元素。

//in viewDidLoad
 isParsingNextString = NO;

//put this inside your begin parsing method
if (isParsingNextString == NO) {
     //parse 1st string URL
}

if (isParsingNextString == YES) {
     //parse 2nd string URL
}

//when your document finishes set the bool to yes    
//now restart your parser inside an if statement so you dont have a parsing loop
if(isParsingNextString == NO) {  
  isParsingNextString = YES;  
  [self parseXML];
}