xml只需要一个元素(iOS中的XML解析)
问题描述:
我必须解析XML并将数据显示在表视图中。xml只需要一个元素(iOS中的XML解析)
这就是我的XML的样子。
<?xml version="1.0"?>
<FacLocation>
<Facility Type="Project Room">
<Code>L435</Code>
<Code>L509C</Code>
</Facility>
</FacLocation>
我必须在tableview单元格中显示L435和L509C。
但是XML只存储最后一条记录,即L509C。
在我的cellForRowAtIndexPath;
RoomCell * cell = [tableView dequeueReusableCellWithIdentifier:@“RoomCell”];
if (!cell)
{
cell =[[[NSBundle mainBundle] loadNibNamed:@"RoomCell" owner:nil options:nil] objectAtIndex:0];
}
NSLog(@"This is NSLog!");
Rooms *rc = [self.roomsArray objectAtIndex:indexPath.row];
[cell.RoomLabel setText:[rc roomCode]];
cell.contentView.backgroundColor = [UIColor colorWithRed:0.75 green:0.93 blue:1 alpha:1];
[self.tableView setSeparatorColor:[UIColor colorWithRed:0.55 green:0.55 blue:0.55 alpha:1]];
return cell;
我在我的didEndElement方法中有这个;
if ([elementName isEqualToString:@"Code"])
{
tempRoom.roomCode = self.tempString;
NSLog(@"tempString (Module Room): %@", tempString);
}
if ([elementName isEqualToString:@"Facility"])
{
[self.roomsArray addObject:tempRoom];
}
现在问题是它只读取最后一条记录。这意味着它只能读取L509C。它丢弃了L435的记录。有什么办法可以保存这两个记录并显示它们吗?
答
您需要在阵列添加这些值,这样
[self.roomsArray addObject:@"L435"];
[self.roomsArray addObject:@"L509C"];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.roomsArray count];
}
编辑
if (!cell)
{
cell =[[[NSBundle mainBundle] loadNibNamed:@"RoomCell" owner:nil options:nil] objectAtIndex:0];
}
NSLog(@"This is NSLog!");
// ------------- Change here
[cell.RoomLabel setText:[self.roomsArray objectAtIndex:indexPath.row]];
cell.contentView.backgroundColor = [UIColor colorWithRed:0.75 green:0.93 blue:1 alpha:1];
[self.tableView setSeparatorColor:[UIColor colorWithRed:0.55 green:0.55 blue:0.55 alpha:1]];
return cell;
我不得不从XML纸张阅读的房间名字后。无论如何,我可以做到这一点,而不需要硬编码? @DharmbirChoudhary –
您应该将对象添加到foundCharacter xml的委托方法中的数组中。 –
谢谢。但我有另一个问题。我更新了我的问题。有什么办法可以保存两条记录吗?因为我使用的这种方法只读取最后一个记录,即L509C。谢谢。 @DharmbirChoudhary –