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.
-
Do you mean you need to remove the repeated data from array?icodebuster– icodebuster2013年06月26日 15:59:01 +00:00Commented Jun 26, 2013 at 15:59
-
2Here's a nice XML library - really simple to use: github.com/ZaBlanc/RaptureXML . . .can be installed via CocoaPods.org too.Jasper Blues– Jasper Blues2013年06月26日 16:00:27 +00:00Commented Jun 26, 2013 at 16:00
3 Answers 3
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];
1 Comment
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
1 Comment
Simple method which i follow to parse xml even complex xml data:
- Include XMLReader library to your project. https://github.com/amarcadet/XMLReader
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];
Use library function to get proper data, seperated tag wise to NSDictionary.
NSDictionary *dict = [XMLReader dictionaryForXMLData:xmldata options:XMLReaderOptionsProcessNamespaces error:&error];
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.
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 .
I handle this issue using exceptional handling.
Overall it will only take 15 minutes to work smoothly to read xml data, even complexly nested xml data.
Also if want to write XML data, i refer using XMLWriter