How to store 3 table parsing value in 3 different

2019-01-29 13:33发布

问题:

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.

My xml looks like this

<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>

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

回答1:

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

XMLReader.h and XMLReader.m

Here is the code

 NSString *xmlString=@"<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>";

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 = "YOURMAIL1@CAFE.COM";
            };
            PurchasesAmount =                 {
                text = "0.0000";
            };
            SalesAmount =                 {
                text = "0.0000";
            };
            "diffgr:id" = Table1;
            "msdata:rowOrder" = 0;
        },
                    {
            CreatedBy =                 {
                text = 2;
            };
            Email =                 {
                text = "YOURMAIL2@CAFE.COM";
            };
            PurchasesAmount =                 {
                text = "0.0000";
            };
            SalesAmount =                 {
                text = "0.0000";
            };
            "diffgr:id" = Table2;
            "msdata:rowOrder" = 0;
        },
                    {
            CreatedBy =                 {
                text = 3;
            };
            Email =                 {
                text = "YOURMAIL3@CAFE.COM";
            };
            PurchasesAmount =                 {
                text = "0.0000";
            };
            SalesAmount =                 {
                text = "0.0000";
            };
            "diffgr:id" = Table3;
            "msdata:rowOrder" = 0;
        }
    );
};
}

and don't forget to disable arc if you are using 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];


回答2:

You can check attribute value in didStartElement, and based on that create different array for each attribute value and add it in dictionary as follows:

  MainDict:[
   Arr1:{
    1,
    YOURMAIL1@CAFE.COM,
    0.0000,
    0.0000

    },
   Arr2: {
    2,
    YOURMAIL2@CAFE.COM,
    0.0000,
    0.0000

    },
so on..
]

Or I can suggest second method where you can use dictionaries to find out different table. If you want to differentiate between data in table1, table2, etc, then you can use dictionaries where each dictionary again represent dictionary, for ex:

MainDict:[

table1:[
CreatedBy: 1,
Email: YOURMAIL1@CAFE.COM,
SalesAmount: 0.0000,
PurchasesAmount: 0.0000
],

table2:[
CreatedBy: 2,
Email: YOURMAIL2@CAFE.COM,
SalesAmount: 0.0000,
PurchasesAmount: 0.0000
],
.
.
.
]

In this way you can differentiate between each dictionary and value of each tag.Even you can use this dictinary in tableview.