2011-08-18 101 views
1

我想從兩個不同的XML中讀取數據來填充我的視圖在iPhone中的字段。iPhone:如何在同一視圖中訪問多個XML

有什麼方法可以在同一個視圖中讀取多個XML嗎?我可以讀取和解析一個XML。

感謝

//使用NSXML解析器

-(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; 

} 
+0

您使用哪種XML解析器?你用什麼代碼來分析XML?沒有這些信息,很難有人回答這個問題。 – 2011-08-18 22:15:24

+0

我已經添加了代碼 – Katy

回答

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]; 
} 
+0

謝謝路易我會嘗試實施它。 – Katy

+0

嘿Louie,第二個XML沒有被解析,應用程序崩潰。控制檯上也沒有錯誤。有什麼建議麼? – Katy

+0

確保你沒有太早釋放你的解析器,並確保你在第二個字符串中解釋了不同的元素類型。 – Louie

相關問題