1

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月19日T09: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月19日T10: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月19日T09: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月19日T10: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月19日T11: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月19日T09: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月19日T10: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.

rmaddy
320k44 gold badges548 silver badges591 bronze badges
asked Jun 26, 2013 at 15:42
2
  • Do you mean you need to remove the repeated data from array? Commented Jun 26, 2013 at 15:59
  • 2
    Here's a nice XML library - really simple to use: github.com/ZaBlanc/RaptureXML . . .can be installed via CocoaPods.org too. Commented Jun 26, 2013 at 16:00

3 Answers 3

1

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];
answered Jun 26, 2013 at 16:26
Sign up to request clarification or add additional context in comments.

1 Comment

@aslam If you find any of the answers useful please upvote it and if answer is correct then please accept it this will help to remove the question from the list of unanswered list of question
0

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

answered Jun 26, 2013 at 17:48

1 Comment

please read the question correctly, The user is able to read the xml data and he needs to sort the parsed result like the sample at the end of the question.
0

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

answered Jun 26, 2013 at 19:03

2 Comments

@yogesh-lolusare-apple please read the question correctly, The user is able to read the xml data and he needs to sort the parsed result like the sample at the end of the question.
@icodebuster, oh its my fault. and i found above answer more appropriate (sorry can't vote waiting to get 15 reputations).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.