0

我是 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;          
        }
    }
}
4

2 回答 2

3

您可以使用 xmlReader,这将使其易于处理。这是文件

XMLReader.hXMLReader.m

这是代码

 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"];

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

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

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

在此处输入图像描述

---------------为每个表使用字典。--------

   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];
于 2013-05-31T07:57:47.097 回答
1

您可以检查 didStartElement 中的属性值,并在此基础上为每个属性值创建不同的数组并将其添加到字典中,如下所示:

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

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

    },
so on..
]

或者我可以建议第二种方法,您可以使用字典查找不同的表。如果您想区分 table1、table2 等中的数据,则可以使用每个字典再次代表字典的字典,例如:

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
],
.
.
.
]

通过这种方式,您可以区分每个字典和每个标签的值。即使您可以在 tableview 中使用此字典。

于 2013-05-31T07:36:55.047 回答