API
API Links
Read:
http://Folderpath/XML/student.php?action=list
Add:
http:// Folderpath
/XML/student.php?action=add&Name=Alex&Age=16&Perc=90
Edit:
http:// Folderpath
/XML/student.php?action=edit&Name=Alex&Age=16&Perc=90.86&StudentId=3
Delete:
http:// Folderpath /XML/student.php?action=delete&StudentId=3
student-list.php
<?php
header ("content-type:
text/xml");
sleep(3);
echo '<?xml
version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<Name>Alan</Name>
<Age>18</Age>
<Perc>88.90</Perc>
</Student>
<Student>
<Name>Bob</Name>
<Age>20</Age>
<Perc>85.60</Perc>
</Student>
<Student>
<Name>Alex</Name>
<Age>16</Age>
<Perc>90.16</Perc>
</Student>
</Students>';
?>
student-list.xml
<?xml
version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<Name>Alan</Name>
<Age>18</Age>
<Perc>88.90</Perc>
</Student>
<Student>
<Name>Bob</Name>
<Age>20</Age>
<Perc>85.60</Perc>
</Student>
<Student>
<Name>Alex</Name>
<Age>16</Age>
<Perc>90.16</Perc>
</Student>
</Students>
AppDelegate.h
@property (nonatomic, retain) NSString *apiURL;
@property (strong, nonatomic) UIWindow *window;
AppDelegate.m
@synthesize
apiURL;
apiURL = @"http://FolderPath/XML/";
ViewController.h
#import <UIKit/UIKit.h>
@class
ip23AppDelegate,
Student;
@interface ip23ViewController : UIViewController <NSXMLParserDelegate,
UITableViewDataSource,
UITableViewDelegate>
{
ip23AppDelegate
*appDel;
Student *aRecord;
NSMutableString
*recordsStr;
NSMutableArray
*recordsArr;
}
@property (nonatomic, retain) IBOutlet
UIActivityIndicatorView *processActView;
@property (nonatomic, retain) IBOutlet UITableView
*recordsTblView;
@end
ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
#import "MyFunctions.h"
#import "StudentCellViewController.h"
@implementation ip23ViewController
@synthesize processActView,
recordsTblView;
- (void)viewDidLoad
{
[super
viewDidLoad];
appDel =
(ip23AppDelegate *)[[UIApplication sharedApplication] delegate];
NSOperationQueue
*queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self selector:@selector(getApiData) object:nil];
[queue
addOperation:operation];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any
resources that can be recreated.
}
#pragma mark - XML Parse
- (void)loadDataInTable
{
processActView.hidden = YES;
[recordsTblView
reloadData];
}
- (void)getApiData
{
processActView.hidden
= NO;
recordsArr =
[[NSMutableArray alloc] init];
NSString
*myApiLink = [NSString stringWithFormat:@"%@student-list.php",
appDel.apiURL];
//NSLog(@"%@", myApiLink);
NSURL *url =
[[NSURL alloc] initWithString:myApiLink];
NSXMLParser
*xParse = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xParse
setDelegate:self];
[xParse parse];
[self
performSelectorOnMainThread:@selector(loadDataInTable) withObject:nil
waitUntilDone:NO];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary
*)attributeDict
{
if([elementName
isEqualToString:@"Student"])
{
aRecord =
[[Student alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
if(!recordsStr)
recordsStr = [[NSMutableString alloc] initWithString:string];
else
[recordsStr appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString
*)qName
{
if([elementName
isEqualToString:@"Students"])
{
return;
}
else
if([elementName isEqualToString:@"Student"])
{
[recordsArr
addObject:aRecord];
aRecord
= nil;
}
else
{
if([elementName isEqualToString:@"Name"] ||
[elementName isEqualToString:@"Age"] ||
[elementName isEqualToString:@"Perc"])
{
[aRecord
setValue:recordsStr forKey:elementName];
}
}
recordsStr = nil;
}
#pragma mark - Table Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return
recordsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString
*CellIdentifier = @"Cell";
UITableViewCell
*cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
Student *sObj =
[recordsArr objectAtIndex:indexPath.row];
StudentCellViewController *studentCellVC = [[StudentCellViewController
alloc] init];
studentCellVC.studentObj = sObj;
[cell.contentView
addSubview:studentCellVC.view];
return cell;
}
@end
MyFunctions.h
#import <Foundation/Foundation.h>
@interface MyFunctions : NSObject
+ (NSString *)trim:(NSString *)stringToTrim;
@end
MyFunctions.m
#import "MyFunctions.h"
@implementation MyFunctions
+ (NSString *)trim:(NSString *)stringToTrim
{
return
[stringToTrim stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
}
@end
Student.h
@property (nonatomic, retain) NSString *Name,
*Age,
*Perc;
Student.m
@synthesize Name,
Age,
Perc;
StudentCellViewController.h
#import <UIKit/UIKit.h>
@class Student;
@interface StudentCellViewController : UIViewController
@property (nonatomic, retain) IBOutlet UILabel *NameLbl,
*AgeLbl,
*PercLbl;
@property (nonatomic, retain) Student *studentObj;
@end
StudentCellViewController.m
#import "StudentCellViewController.h"
#import "Student.h"
#import "MyFunctions.h"
@implementation StudentCellViewController
@synthesize NameLbl,
AgeLbl,
PercLbl;
@synthesize studentObj;
- (void)viewDidLoad
{
[super
viewDidLoad];
NameLbl.text =
[MyFunctions trim:studentObj.Name];
AgeLbl.text =
[NSString stringWithFormat:@"%@yr", [MyFunctions
trim:studentObj.Age]];
PercLbl.text =
[NSString stringWithFormat:@"%@%%", [MyFunctions
trim:studentObj.Perc]];
}
No comments:
Post a Comment