本文介绍了如何在iphone中的3个不同数组中存储3个表解析值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone编程的新手,我正在使用NSXmlParser来解析数据并将其存储在一个数组中,但是我想将3个表数据存储在3个不同的数组中,任何人都可以告诉我如何解析数据并将其存储在3个不同的数组中。

I am new to iPhone programming and I am using NSXmlParser to parse the data and store it in an array but I have 3 tables data I want to store that in 3 different arrays, can anybody tell me how can I parse the data and store it in 3 different array.

我的xml看起来像这样

<Result>
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <CreatedBy>1</CreatedBy>
        <Email>[email protected]</Email>
        <SalesAmount>0.0000</SalesAmount>
        <PurchasesAmount>0.0000</PurchasesAmount>
      </Table>
 <Table diffgr:id="Table2" msdata:rowOrder="0">
        <CreatedBy>2</CreatedBy>
        <Email>[email protected]</Email>
        <SalesAmount>0.0000</SalesAmount>
        <PurchasesAmount>0.0000</PurchasesAmount>
      </Table>
 <Table diffgr:id="Table3" msdata:rowOrder="0">
        <CreatedBy>3</CreatedBy>
        <Email>[email protected]</Email>
        <SalesAmount>0.0000</SalesAmount>
        <PurchasesAmount>0.0000</PurchasesAmount>
      </Table>
    </Result>

使用下面的代码,我可以将所有已解析的数据存储在一个数组中。

Using below code I am able to store all parsed data in one array.

-(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;          
        }
    }
}


推荐答案

你可以使用xmlReader来轻松处理。这是文件

you can use xmlReader that will make it easy to handle.Here is the files

这是代码

 NSString *xmlString=@"<Result><Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"><CreatedBy>1</CreatedBy><Email>[email protected]</Email><SalesAmount>0.0000</SalesAmount><PurchasesAmount>0.0000</PurchasesAmount></Table><Table diffgr:id=\"Table2\" msdata:rowOrder=\"0\"><CreatedBy>2</CreatedBy><Email>[email protected]</Email><SalesAmount>0.0000</SalesAmount><PurchasesAmount>0.0000</PurchasesAmount></Table><Table diffgr:id=\"Table3\" msdata:rowOrder=\"0\"><CreatedBy>3</CreatedBy><Email>[email protected]</Email><SalesAmount>0.0000</SalesAmount><PurchasesAmount>0.0000</PurchasesAmount></Table></Result>";

NSDictionary *dics=[[NSDictionary alloc]initWithDictionary:[XMLReader dictionaryForXMLString:xmlString error:nil]];

NSLog(@"dics is %@",dics);
NSArray *tableDicsArray=[[dics valueForKey:@"Result"] valueForKey:@"Table"];

这是你的输出------------

And here is your output------------

dics is {
Result =     {
    Table =         (
                    {
            CreatedBy =                 {
                text = 1;
            };
            Email =                 {
                text = "[email protected]";
            };
            PurchasesAmount =                 {
                text = "0.0000";
            };
            SalesAmount =                 {
                text = "0.0000";
            };
            "diffgr:id" = Table1;
            "msdata:rowOrder" = 0;
        },
                    {
            CreatedBy =                 {
                text = 2;
            };
            Email =                 {
                text = "[email protected]";
            };
            PurchasesAmount =                 {
                text = "0.0000";
            };
            SalesAmount =                 {
                text = "0.0000";
            };
            "diffgr:id" = Table2;
            "msdata:rowOrder" = 0;
        },
                    {
            CreatedBy =                 {
                text = 3;
            };
            Email =                 {
                text = "[email protected]";
            };
            PurchasesAmount =                 {
                text = "0.0000";
            };
            SalesAmount =                 {
                text = "0.0000";
            };
            "diffgr:id" = Table3;
            "msdata:rowOrder" = 0;
        }
    );
};
}

如果您使用ARC,请不要忘记禁用arc 。

---------------为每个表使用词典.-------

---------------Use dictionaries for each table.-------

   NSDictionary *tbl1= [tableDicsArray objectAtIndex:0];
   NSDictionary *tbl2= [tableDicsArray objectAtIndex:1];
   NSDictionary *tbl3= [tableDicsArray objectAtIndex:2];

    NSArray *tbl1Keys=[[tbl1 allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSArray *tbl2Keys=[[tbl2 allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSArray *tbl3Keys=[[tbl3 allKeys] sortedArrayUsingSelector:@selector(compare:)];


    //you can use these keys to pass no of rows in you table.

    //and use dictionary in cellforRow as give below:-------

    NSString *key=[tbl1Keys objectAtIndex:indexPath.key];
    cell.textLabel.text=[tbl1 valueForKey:key];

这篇关于如何在iphone中的3个不同数组中存储3个表解析值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:29