我是Xcode的初学者,我尝试创建一个应用程序来下载XML文件,解析它并在数组中添加每个获得的元素。这个数组然后被一个表视图用来创建新的行。不幸的是它没有奏效。在NSLog几乎每一步之后,我注意到应用程序下载和解析文件,但是当涉及到将数据添加到数组时,应用程序清空数组并使用最后一个元素填充数据,数量与最后一个元素一样多元素在开始。即使如此,表视图应该有一些行,但是当我离开解析例程时,似乎该数组仅填充了“\ n”,这是我不明白的。我分配和正确初始化数组: myArray = [[NSMutableArray alloc]init];
我对象添加到阵列: [myArray addObject:@"Hello"];
XML解析后的数组为空
我准确的,我使用的Xcode 4.6.2。对于iOS 6,我是初学者(请不要拍我;))
感谢您的帮助!
我使用的XML文件可以在ilgl.lgl.lu/Missings_Data/Missings_Data.xml
找到这里的部分代码我使用:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Released_classes"]) {
prof = [[NSMutableArray alloc]init];
classe = [[NSMutableArray alloc]init];
span = [[NSMutableArray alloc]init];
service = [[NSMutableArray alloc]init];
currentElement = [[NSMutableString alloc] init];
NSLog(@"Data containers initialized");
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// ignore root and empty elements
if ([elementName isEqualToString:@"Missing_Class"] || [elementName isEqualToString:@"Released_classes"]) return;
if ([elementName isEqualToString:@"Classe"]) {
[classe addObject:currentElement];
NSLog(@"Added element to Classe: %@", currentElement);
NSLog(@"Elements in class: %@", classe);
return;
}
if ([elementName isEqualToString:@"Professeur"]) {
[prof addObject:currentElement];
NSLog(@"Added element to Prof: %@", currentElement);
NSLog(@"Elements in prof: %@", classe);
return;
}
if ([elementName isEqualToString:@"Lecon_libérée"]) {
[span addObject:currentElement];
NSLog(@"Added element to Span: %@", currentElement);
NSLog(@"Elements in span: %@", classe);
return;
}
if ([elementName isEqualToString:@"Service"]) {
[service addObject:currentElement];
NSLog(@"Added element to Service: %@", currentElement);
NSLog(@"Elements in service: %@", classe);
return;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[currentElement setString:string];
}
#pragma mark - Table View Data Organization
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
NSLog(@"Number of sections: predefined 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"Number of rows: %i", [classe count]);
return [classe count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSString *cellText = [classe objectAtIndex:indexPath.row];
cell.textLabel.text = cellText;
NSLog(@"Cell being created: %@", cellText);
return cell;
}
编辑 由于北斗星,我在parser:didEndElement:
的每个if
条件的末尾加上了currentElement = nil
,并且他给了我的验证。它现在完美地工作!感谢所有试图帮助的人:)
请发布您正在使用的代码。 – Jim 2013-05-12 14:33:07
我们可以有一个下载的文件的样本? – Undo 2013-05-12 14:42:32
对于XMLParser代码,我使用了Apple提供的代码(https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html#//apple_ref/doc/uid/20002265-BCIJFGJI)。 – 2013-05-12 15:04:26