iPhone: How to access multiple XMLs in the same view
I am trying to read data from two different XML's to populate fields of my View in iPhone.
Is there any way to read multiple XML's in the same view? I can read and parse a single XML.
Thanks
//Using the 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开发者_Go百科: (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;
}
Use a Boolean, name it isParsingNextString to parse a different string and elements.
//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];
}
精彩评论