我是 iPhone 编程新手,我正在使用 NSXmlParser 解析数据并将其存储在一个数组中,但我有 3 个表数据我想将其存储在 3 个不同的数组中,谁能告诉我如何解析数据并将其存储在 3 个不同的数组中。
我的 xml 看起来像这样
<Result>
<Table diffgr:id="Table1" msdata:rowOrder="0">
<CreatedBy>1</CreatedBy>
<Email>YOURMAIL1@CAFE.COM</Email>
<SalesAmount>0.0000</SalesAmount>
<PurchasesAmount>0.0000</PurchasesAmount>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="0">
<CreatedBy>2</CreatedBy>
<Email>YOURMAIL2@CAFE.COM</Email>
<SalesAmount>0.0000</SalesAmount>
<PurchasesAmount>0.0000</PurchasesAmount>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="0">
<CreatedBy>3</CreatedBy>
<Email>YOURMAIL3@CAFE.COM</Email>
<SalesAmount>0.0000</SalesAmount>
<PurchasesAmount>0.0000</PurchasesAmount>
</Table>
</Result>
使用下面的代码,我可以将所有解析的数据存储在一个数组中。
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
arr_info=[NSMutableArray arrayWithObjects:@"CreatedBy",@"Email",@"SalesAmount",@"PurchasesAmount",nil];
for (i=0; i<[arr_info count]; i++)
{
if( [elementName isEqualToString:[arr_info objectAtIndex:i]])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
xmlResults = YES;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if( xmlResults )
{
[soapResults appendString: string];
NSLog(@"soap result %@",soapResults);
[arr_detail addObject:soapResults];
NSLog(@"gg %@",arr_detail);
}
[tableView reloadData];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
for (i=0; i<[arr_info count]; i++)
{
if( [elementName isEqualToString:[arr_info objectAtIndex:i]])
{
xmlResults = FALSE;
switch ([arr_detail count]) {
case 1:
l1.text=[arr_detail objectAtIndex:[arr_detail count]-1];
NSLog(@"%@",[arr_detail objectAtIndex:[arr_detail count]-1]);
break;
case 2:
l2.text=[arr_detail objectAtIndex:[arr_detail count]-1];
NSLog(@"%@",[arr_detail objectAtIndex:[arr_detail count]-1]);
break;
case 3:
l3.text=[arr_detail objectAtIndex:[arr_detail count]-1];
NSLog(@"%@",[arr_detail objectAtIndex:[arr_detail count]-1]);
break;
case 4:
l4.text=[arr_detail objectAtIndex:[arr_detail count]-1];
NSLog(@"%@",[arr_detail objectAtIndex:[arr_detail count]-1]);
break;
}
[tableView reloadData];
soapResults = nil;
}
}
}