Followers

Thursday, August 26, 2010

Parse xml

- (ParseXml *) initparserxml:(AdMeAppDelegate *) appDelegate1 {
[super init];
appDelegate = (AdMeAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate = appDelegate1;
NSLog(@"inside parse XML in parse XML");
return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {

if([elementName isEqualToString:@"adme"]) {
//Initialize the array.
appDelegate.listData = [[NSMutableArray alloc] init];
} else if([elementName isEqualToString:@"campaign"]) {

//Initialize the campaign.
adetails = [[XmlData alloc] init];

//Extract the attribute here.
adetails.campaign_id = [[attributeDict objectForKey:@"campaign_id"] stringValue];

NSLog(@"Reading id value :%i", adetails.campaign_id);
}

NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

currentElementValue = [[NSMutableString alloc] initWithString:string];

/*if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
*/
NSLog(@"Processing Value: %@", currentElementValue);

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if([elementName isEqualToString:@"adme"])
{

if([appDelegate.listData count] == 0) {
appDelegate.listData = [[NSMutableArray alloc] init];
adetails = [[XmlData alloc] init];
adetails.campaign_name = @"No campaign in list";
[appDelegate.listData addObject:adetails];
[adetails release];
adetails = nil;
}
return;
}


//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the campaign object to the array
// and release the object.
if([elementName isEqualToString:@"campaign"]) {
[appDelegate.listData addObject:adetails];
[adetails release];
adetails = nil;
}
else
[adetails setValue:currentElementValue forKey:elementName];

[currentElementValue release];
currentElementValue = nil;
}
------------------------------------------------------------------------------

view control
------------------------------------------------------------------------------

- (void)loadView {
}
*/

- (void)parseXML:(NSData *)data {
NSLog(@"inside parse XML in test list");
NSAttributedString *output = [NSString stringWithCString:[data bytes] length:[data length]];
NSString *mystr = [NSString localizedStringWithFormat:@"%@data:",output];
NSLog(@"data is %@",mystr);
printf("HTTP RESPONSE : - %s\n",&mystr);

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
ParseXml *parser = [[ParseXml alloc] initparserxml:appDelegate];
[xmlParser setDelegate:parser];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];


BOOL success = [xmlParser parse];
if(success)
NSLog(@"no errors");
else
NSLog(@"error !!!");
}


- (void)getListView {
NSData *data;
NSLog(@"Inside getList view");
NSString *listview_url = [NSString stringWithFormat:@"%@%@",appDelegate.HTTPS_BASE_URL,appDelegate.XML_VIEW_URL];
NSURL *listurl = [NSURL URLWithString:listview_url];
NSHTTPURLResponse *response;
NSError *error;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:listurl];
[request setHTTPMethod:@"GET"];
[request setHTTPShouldHandleCookies:YES];
request = [NSMutableURLRequest requestWithURL:listurl];


data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if([response statusCode] == 200) {
[self parseXML:data];
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning !" message:@"Got 200 status" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
//[alert show];

} else {
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning !" message:@"Error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
//[alert show];
}
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
inc = 0.0;
appDelegate = (AdMeAppDelegate *) [[UIApplication sharedApplication] delegate];
[self getListView];
tableView.backgroundColor = [UIColor blackColor];
[super viewDidLoad];
}

1 comment:

oneprakash said...

Hello Pradeep,

I am Prakash, a startup evangelist.

I am part of Chronus and Whizrx - these are startups having SDC in Chennai and headquarters in USA.

Like to be connected with people who are passionate about technology / startups.


--
Thanks,
Prakash
Startup Evangelist
M 90030 33998
Skype ID - bethavandu

My Blog - http://chennaistartup.wordpress.com/
Linkedin - http://in.linkedin.com/in/kinetprakash
Facebook - http://on.fb.me/bgQhYp
Twitter - http://bit.ly/cQ8FN3