Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I use the following code for XML parsing but it is not working.

**1)Channel.h**

    enter code here

#import 
@interface Channel : NSObject {
	
	
	NSInteger		ChannelID;
	NSString		*title;
	NSString		*urlName;
	NSString		*imgname;
	
	
	
}

@property (nonatomic, readwrite) NSInteger ChannelID;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *urlName;
@property (nonatomic, retain) NSString *imgname;
@end

**Channel.m**


#import "Channel.h"
#import "ParseXMLFileAppDelegate.h"

@implementation Channel

@synthesize title; 
@synthesize ChannelID; 
@synthesize urlName;
@synthesize imgname;

- (void) dealloc {
	
	[title release];
	[urlName release];
	[imgname release];
	[super dealloc];
}

@end

**2) XMLParser.h**


#import 

@class ParseXMLFileAppDelegate, Channel ;

@interface ChannelXMLParser : NSObject {

	ParseXMLFileAppDelegate	 *appDelegate;
	NSMutableString		*currentElementValue;
	Channel			*aChannel;
	
}

- (ChannelXMLParser *) initXMLParser;

@end

 *XMLParser.m*


#import "ChannelXMLParser.h"
#import "ParseXMLFileAppDelegate.h"
#import "Channel.h"


@implementation ChannelXMLParser

/*
 return object
 */

- (ChannelXMLParser *) initXMLParser {
	
	[super init];
	
	appDelegate = [ParseXMLFileAppDelegate getSharedDelegate];
	
	return self;
}


#pragma mark NSXMLParser methods
/* 
 XMLParser delegate methods
 
 */

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
//	NSLog(@"namespace: %@",namespaceURI);
		
	if([elementName isEqualToString:@"Channels"]) {
		//Initialize the array.
		if(appDelegate.Channels == nil)
			appDelegate.Channels = [[NSMutableArray alloc] init];
		
		
	}
	else if([elementName isEqualToString:@"Channel"]) {
		
		//Initialize the Channel.
		aChannel = [[Channel alloc] init];
		
		//Extract the attribute here.
		aChannel.ChannelID = [[attributeDict objectForKey:@"id"] integerValue];
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
		
	if(!currentElementValue) 
		currentElementValue = [[NSMutableString alloc] initWithString:string];
	else
		[currentElementValue appendString:string];
		
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	
	if([elementName isEqualToString:@"Channels"])
		
		return;
	
	if([elementName isEqualToString:@"Channel"]) {
		
		[appDelegate.Channels addObject:aChannel];
		[aChannel release];
		aChannel = nil;
		NSLog(@"count : %d",[appDelegate.Channels count]);
	}
	else {
		[aChannel setValue:currentElementValue forKey:elementName];
	}
		
	[currentElementValue release];
	currentElementValue = nil;
}

- (void) dealloc {
	
	[aChannel release];
	[currentElementValue release];
	[super dealloc];
}

@end

**3) MyXMLData.h**


#import 

@class ParseXMLFileAppDelegate;
@interface MyXMLData : UIViewController<uitableviewdelegate,uitableviewdatasource> {
	
	
	UITableView				*tableView;
	ParseXMLFileAppDelegate	*appDelegate ;

}
@property(nonatomic,retain)UITableView *tableView;
-(id)initWithTabBar;
-(void)manageTableData;

@end


**MyXMLData.m**


#import "MyXMLData.h"
#import "ChannelXMLParser.h"
#import "Channel.h"
#import "ParseXMLFileAppDelegate.h"

@implementation MyXMLData
@synthesize tableView;


-(id)initWithTabBar{
	
	
	appDelegate = [ParseXMLFileAppDelegate getSharedDelegate];


	[self.navigationItem  setTitle : @"Show My XML"];
	
	
	//  Configure the Left button
	self.navigationItem.leftBarButtonItem =
	[[[UIBarButtonItem alloc] 
	  initWithTitle:@"Get XML" style:UIBarButtonItemStyleDone 
	  target:self action:@selector(xmlDataLoading)] autorelease];
	
	
	return self;
}



#pragma mark Table view methods
	
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
		
		
		return 1;
}
	
	
- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section {
	
		
	
		return [appDelegate.Channels count];
		
}
	
	
- (UITableViewCell *)tableView:(UITableView *)tableView1 
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSLog(@" cell for row");
		
		static NSString *CellIdentifier = @"CellIdentifier";
		UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
											   reuseIdentifier:CellIdentifier] autorelease];
			
		}
	
		Channel *aChannel = [appDelegate.Channels objectAtIndex:indexPath.row];
		NSString *title = aChannel.title;
		
		[cell.textLabel setText:title];
		
		return cell;
}
	



/*
 
 
 Initiate the parser here
 
 */
-(BOOL)xmlDataLoading{
	
	[appDelegate.Channels removeAllObjects];
	const NSString *kSelectedGenreFilePath =
					@"http://Enter your website for retrieve xml";
	NSString *filePathName = 
	[NSString stringWithFormat:
	 @"%@Pop.xml",kSelectedGenreFilePath];
	
	NSURL *url = nil;
	url = [[NSURL alloc] initWithString:filePathName];
	
	
	NSXMLParser *xmlParser = [[NSXMLParser alloc] 
							  initWithContentsOfURL:url];
	[url release];
	ChannelXMLParser *parser = [[ChannelXMLParser alloc] initXMLParser];
	[xmlParser setDelegate:parser];
	
	BOOL success = [xmlParser parse];
	
	
	
	[xmlParser release];
	[parser release];
	if(success){
		
		[self manageTableData]; 
		return YES;
	}
	else{
		return NO;
	}
	
	
}

-(void)manageTableData{
	
	if(self.tableView == nil){

	self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
	self.tableView.backgroundColor = [UIColor clearColor];
	
	self.tableView.delegate = self;
	self.tableView.dataSource = self;
	CGRect tableFrame = CGRectMake(0.0,0.0,320.0,480.0);
	self.tableView.frame = tableFrame;
	self.view = self.tableView;
	}
	
}


- (void)dealloc {
    [super dealloc];
}

@end

**4) ParseXMLFileAppDelegate.h**

#import 

@interface ParseXMLFileAppDelegate : NSObject <uiapplicationdelegate> {
	
	UINavigationController  *localNavigationController;
	NSMutableArray	               *Channels;
    UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSMutableArray *Channels;


+(ParseXMLFileAppDelegate *) getSharedDelegate;

@end

**ParseXMLFileAppDelegate.m**

#import "ParseXMLFileAppDelegate.h"
#import "MyXMLData.h"

@implementation ParseXMLFileAppDelegate

@synthesize window,Channels;


- (void)applicationDidFinishLaunching:(UIApplication *)application { 
	
	
	MyXMLData *myXMLData;
	myXMLData = [[MyXMLData alloc] initWithTabBar];

	localNavigationController =
	[[UINavigationController alloc] initWithRootViewController:myXMLData];


	[window addSubview:localNavigationController.view];
   
    [window makeKeyAndVisible];
}

+(ParseXMLFileAppDelegate *) getSharedDelegate{
	
	return [[UIApplication sharedApplication]delegate];
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end</uiapplicationdelegate>
Posted
Updated 18-Aug-11 2:15am
v2
Comments
LittleYellowBird 18-Aug-11 8:16am    
In what way does it 'not work'? You need to explain what your problem is and what you have done to try and fix it, then perhaps someone can advise.
Malli_S 25-Aug-11 4:15am    
In what sense it's 'not working' ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900