Aslam
Aslam

Reputation: 231

How to parse data using below xml data in iPhone

I am new to i phone programming.I have xml data which i have show as below.Using that data i want to display particular date data having 2 tables how can i display particular date data in single row.In next data 2012-06-20 having 3 tables how can i display 3tables data in second row.How can i parse that data,Tell me logic for that

<Result>
          <Table diffgr:id="Table1" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T09:01:54-06:00</CafeDateTime>
            <CreatedBy>1</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>100.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>
         <Table diffgr:id="Table2" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T10:01:54-06:00</CafeDateTime>
            <CreatedBy>1</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>100.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>
         </Table> <Table diffgr:id="Table3" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T09:01:54-06:00</CafeDateTime>
            <CreatedBy>2</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>200.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>

         </Table> <Table diffgr:id="Table4" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T10:01:54-06:00</CafeDateTime>
            <CreatedBy>2</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>200.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>
         </Table> <Table diffgr:id="Table5" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T11:01:54-06:00</CafeDateTime>
            <CreatedBy>2</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>200.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>
         </Table> <Table diffgr:id="Table6" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T09:01:54-06:00</CafeDateTime>
            <CreatedBy>3</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>150.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>
         </Table> <Table diffgr:id="Table7" msdata:rowOrder="2">
            <CafeDateTime>2012-09-19T10:01:54-06:00</CafeDateTime>
            <CreatedBy>3</CreatedBy>
            <Email>[email protected]</Email>
            <AdminPassword>admin1</AdminPassword>
            <SalesDailyAmount>150.0000</SalesDailyAmount>
            <PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
            </Table>
     </Result>


    2012-09-19 ->Having 2 tables(i.e Table1,Table2)
    2013-09-20 ->Having 3 tables(i.e Table3,Table4,Table5)
    2013-09-21 ->Having 2 tables(i.e Table6,Table7)

Now what is my question is In above xml data i have total 7 tables in that table1,table2 data belong to 2012-09-19 date.I want to parse data and display in view like this below

                                  CreateBy  SalesDailyAmount  PurchasesDailyAmount
   2012-09-19 ->Want to display      1           100                0.0000

Using below code

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
       attributes: (NSDictionary *)attributeDict
    {
         if( [elementName isEqualToString:@"CreateBy"])
        {
               if(!soapResults)
            {
                soapResults = [[NSMutableString alloc] init];
            }
            xmlResults = YES;
        }
        if( [elementName isEqualToString:@"SalesDailyAmount"])
        {
               if(!soapResults)
            {
                soapResults = [[NSMutableString alloc] init];
            }
            xmlResults = YES;
        }      
        if( [elementName isEqualToString:@"PurchasesDailyAmount"])
        {
               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);
            NSLog(@"gcvxg %@",arr_info);
        }
        [tableView reloadData];
    }
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
         if( [elementName isEqualToString:@"CreateBy"])
                {
                    xmlResults = FALSE;
                    [aslam4 addObject:soapResults];
                    NSLog(@"hello %@",aslam4);
                    soapResults = nil;
                }
                if( [elementName isEqualToString:@"SalesDailyAmount"])
                {
                    xmlResults = FALSE;
                    [aslam5 addObject:soapResults];            
                    NSLog(@"hello %@",aslam5);
                    soapResults = nil;
                }
              if( [elementName isEqualToString:@"PurchasesDailyAmount"])
                {
                    xmlResults = FALSE;
                    [aslam6 addObject:soapResults];
                      NSLog(@"hello %@",aslam6);
                    soapResults = nil;
                }
            }
    }

Using above code i can able to get data like this below

 OutPut
 CreateBy  SalesDailyAmount  PurchasesDailyAmount
     1           100                0.0000
     1           100                0.0000
     2           200                0.0000
     2           200                0.0000
     2           200                0.0000
     3           150                0.0000
     3           150                0.0000

But i want to display like this below

        CreateBy  SalesDailyAmount  PurchasesDailyAmount
  2012-09-19 ->Want to display      1           100                0.0000
  2012-09-20 ->Want to display      2           200                0.0000
  2012-09-21 ->Want to display      3           150                0.0000

I don't want to display multiple time.In 2012-09-19 having 2 tables but both having same data but i don't want to display multiple times. similarly 2012-09-20 having 3tables here also i don't want to display 3 times i want to display in 3tables data in one row. Overall i want to say that Right now in array its storing data like this below Array ={100,100,200,200,200,150,150} but i don't want like this i want to store like this Array ={100,200,150} Can please tell me how to write logic for that.

Upvotes: 1

Views: 486

Answers (3)

Yogesh Lolusare
Yogesh Lolusare

Reputation: 2212

Simple method which i follow to parse xml even complex xml data:

  1. Include XMLReader library to your project. https://github.com/amarcadet/XMLReader
  2. Pass your xml data or xml file path to convert xml data to NSdata .

    NSString *path = [[NSBundle mainBundle] pathForResource:@"xmlExample" ofType:@"xml"];

    NSData *xmlData = [NSData dataWithContentsOfFile:path];

  3. Use library function to get proper data, seperated tag wise to NSDictionary.

    NSDictionary *dict = [XMLReader dictionaryForXMLData:xmldata options:XMLReaderOptionsProcessNamespaces error:&error];

  4. Now use NSDictionary to work for you

    NSArray *arr = [dict valueforkey: @"xtag.ytag.z.tag"];

    you will get array/array of array of values under ztag .

    NSArray *arr = [dict valueforkey: @"xtag.ytag"];

    you will get array/array of array of values under ytag.

  5. It is working great for me in many project's the issue is "Runtime error in our logic" NSDictionary value for key may return array if there is single row or array else array of array .

  6. I handle this issue using exceptional handling.

  7. Overall it will only take 15 minutes to work smoothly to read xml data, even complexly nested xml data.

  8. Also if want to write XML data, i refer using XMLWriter

Upvotes: 0

user2261595
user2261595

Reputation:

According to your requirement, i think happle is good solution for ur problems. For using the happle class you have to download from github. The important part is concept of xpath. so be sure to use of xpath.

To retrieve the date

     NSString *cafeDateTime = @"//table"; 
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {    
    cafeDateTime = [element objectForKey:@"cafeDateTime"];

}

so on.. For more plz visti this site http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

Upvotes: 0

icodebuster
icodebuster

Reputation: 8964

In order to get the unique values from array use the follwing method.

// Your array containing all values
NSArray *dataListArray = [[NSArray alloc] initWithObjects:@"100", @"100", @"200", @"200", @"200", @"300", @"300", @"300", nil];

// Array containing unique data.
NSArray *uniqueDataArray = [[NSSet setWithArray:dataListArray] allObjects];

Upvotes: 1

Related Questions