在AFN中使用NSXMLParser解析服務器返回的XML數(shù)據(jù)

服務器返回的XML格式:


因為蘋果沒有提供直接獲取xml開始標簽和結束標簽中間的字符串,雖然提供了
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
這個方法獲取中間的字符串,但是這個字符串包含了空格和回車,所以要在這個方法中進行過濾。

源代碼如下:
#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標簽, 創(chuàng)建一個模型
84
85 if (self.itemDict != nil && self.itemArray != nil) {
86 [self.itemArray addObject:self.itemDict];
87 [self.items addObject:self.itemArray];
88 SPLog(@"%@", self.itemDict);
89 }
90
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;
106
107 }
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

如果報錯:可能原因

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
 manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; // 返回XML時,這句話不寫,會報錯。
Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容