在AFN中使用NSXMLParser解析服务器返回的XML数据
服务器返回的XML格式:
因为苹果没有提供直接获取xml开始标签和结束标签中间的字符串,虽然提供了
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string这个方法获取中间的字符串,但是这个字符串包含了空格和回车,所以要在这个方法中进行过滤。
源代码如下:
8 9 #import "RecommendController.h" 11 #import "SPHTTPRequestTool.h" 13 #import "UIImageView+WebCache.h" 14 @interface RecommendController ()<NSXMLParserDelegate> 15 16 @property (nonatomic, strong)NSMutableArray *items; 17 18 @property (nonatomic, strong)NSMutableDictionary *itemDict; 19 20 @property (nonatomic, strong)NSMutableArray *itemArray; 21 22 @property (nonatomic, copy)NSString *itemName; // 记录标签名 23 24 @end 25 26 @implementation RecommendController 37 - (void)viewDidLoad 38 { 39 [super viewDidLoad]; 46 [self loadNewRecommend]; 47 self.tableView.delegate = self; 48 self.tableView.dataSource = self; 49 } 50 51 - (void)loadNewRecommend 52 { 53 54 NSDictionary *dict = [NSDictionary dictionary]; 55 [SPHTTPRequestTool GET:@"http://jackgo.cn/jackgo/lab/v1.3/getitems.php?refresh=1&&earliest=(null)" params:dict success:^(NSXMLParser *parser) { 56 // 2.设置代理 57 parser.delegate = self; 58 59 // 3.开始解析 60 [parser parse]; // 卡住(解析完毕才会返回) 61 62 [self.tableView reloadData]; 63 } failure:^(NSError *error) { 64 SPLog(@"%@",error); 65 }]; 66 67 } 68 69 - (void)parserDidStartDocument:(NSXMLParser *)parser 70 { 71 SPLog(@"parser = %@", parser); 72 } 73 74 75 /** 76 * 解析到一个元素的开头时调用 <items> 77 */ 78 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 79 { 80 81 if ([@"items" isEqualToString:elementName]) { // 解析到一个items标签 82 self.items = [NSMutableArray array]; 83 } else if ([@"item" isEqualToString:elementName]) { // 解析到一个item标签, 创建一个模型 93 self.itemArray = [NSMutableArray array]; 94 self.itemDict = [NSMutableDictionary dictionary]; 95 }else 96 self.itemName = elementName; 97 } 98 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 99 { 100 if([string isEqualToString:@"\n"] || [string isEqualToString:@" "] || [string isEqualToString:@"\n\n"]) return; 105 self.itemDict[self.itemName] = string; 107 }
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if (self.itemDict != nil && self.itemArray != nil && [self.secondElement isEqualToString:elementName]) { [self.itemArray addObject:self.itemDict]; [self.items addObject:self.itemArray]; SPLog(@"%@", self.itemDict); } }
123 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 124 { 125 #warning Potentially incomplete method implementation. 126 127 return 1; 128 } 129 130 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 131 {135 return self.items.count; 136 } 137 138 139 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 140 { 142 UITableViewCell *cell = [[UITableViewCell alloc] init]; 147 NSArray *array = self.items[indexPath.row]; 148 NSDictionary *dict = [array lastObject]; 149 cell.textLabel.text = dict[@"intro"]; 150 cell.textLabel.numberOfLines = 0; 151 [cell.imageView setImageWithURL:dict[@"picurl"] placeholderImage:[UIImage imageNamed:@"error"]]; 152 / 153 return cell; 154 } 155 156 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 157 { 158 return 70; 159 } 210 211 @end
如果报错:可能原因
使用AFN解析时,
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; // 返回XML时,这句话不写,会报错。