進出口搜索解析所有從這個簡單的XML文件的N個標籤屬性「UNIT1」和「UNIT2」,然後把它們放在一個UITableView:解析XML屬性與的NSXMLParser
<xml>
<meta></meta>
<time1>
<product>
<value1 id="id1Time1" unit1="unit1Time1" number1="number1Time1"/>
<value2 id="id2Time1" unit2="unit2Time1" number2="number2Time1"/>
</product>
</time1>
<time2>
<product>
<value1 id="id1Time2" unit1="unit1Time2" number1="number1Time2"/>
<value2 id="id2Time2" unit2="unit2Time2" number2="number2Time2"/>
</product>
</time2>
...
<timeN>
<product>
<value1 id="id1TimeN" unit1="unit1TimeN" number1="number1TimeN"/>
<value2 id="id2TimeN" unit2="unit2TimeN" number2="number2TimeN"/>
</product>
</timeN>
</xml>
我使用的NSXMLParser,自委託,和我使用這個代碼:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"value1"]) {
arrayUnit1 = [[NSMutableArray alloc] init];
stringUnit1 = [attributeDict objectForKey:@"unit1"];
[arrayUnit1 addObject:stringUnit1];
}
if
([elementName isEqualToString:@"value2"]) {
arrayUnit2 = [[NSMutableArray alloc] init];
stringUnit2 = [attributeDict objectForKey:@"unit2"];
[arrayUnit2 addObject:stringUnit2];
}
}
要填充值1和值2的UITableView我使用:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayUnit1 count]; // <--- I Know, here is the problem!
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayUnit1 objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[arrayUnit2 objectAtIndex:indexPath.row]];
return cell;
}
好,解析是完美的,我也可以的NSLog每N stringValues但在我的UITableView我只能看到一行用arrayUnit N(在這個例子中unit1TimeN和unit2TimeN的最後的值)。那麼我怎樣才能用EVERY值填充表格,我的數組的所有N值?也許我需要實現 - (void)解析器:(NSXMLParser *)解析器didEndElement:?謝謝!
是'value1'代碼中的'number1'在xml中嗎? – Eimantas
對不起,我現在編輯了,value1是value1 ...; =) – Huxley