我想從兩個不同的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;
}
您使用哪種XML解析器?你用什麼代碼來分析XML?沒有這些信息,很難有人回答這個問題。 – 2011-08-18 22:15:24
我已經添加了代碼 – Katy