Wednesday, November 10, 2010

XML file data as array in Cocoa using XML Parser

Add a xml file in your project

XML File - myxml.xml

<?xml version="1.0" encoding="utf-8" ?>
<data>
<object>
<name>English</name>
<age>24</age>
</object>
<object>
<name>Akram</name>
<age>20</age>
</object>
<object>
<name>John</name>
<age>30</age>
</object>
</data>


Code of file XMLParser.h


#import <UIKit/UIKit.h>
@interface XMLParser : NSObject {
NSMutableString *currentElementValue;
NSMutableArray *records;
NSMutableDictionary *data;
}
-(void)parseDataWithCase;
-(NSMutableArray *)records;
@end



Code of file XMLParser.m


#import "XMLParser.h"
@implementation XMLParser
-(void)parseDataWithCase{
NSString *filePath = @"myxml.xml";
// initializing array to be obtained after parsing
if(records)
[records removeAllObjects];
else
records=[[NSMutableArray alloc] init];

// xml parsing
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filePath]]];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:YES];
BOOL success;
success = [parser parse]; // return value not used
[parser release];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
}
- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string{
currentElementValue = [[NSString alloc] initWithString:string];
if( [currentElementValue isEqualToString:@"\n"] || [currentElementValue isEqualToString:@"NULL"] || [currentElementValue isEqualToString:@"(null)"] ){
[currentElementValue release];
}

}
- (void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName{
if ([elementName isEqualToString:@"data"]){
[data removeAllObjects];
} else if ([elementName isEqualToString:@"object"]){
[records addObject:[[data mutableCopy] autorelease]];
[data removeAllObjects];
} else {
if(data){
}else{
data=[[NSMutableDictionary alloc] initWithCapacity:2];
}
[data setObject:currentElementValue forKey:elementName];
[currentElementValue release];
}
}
-(NSMutableArray *)records{
return records;
}
- (void) dealloc {
[records release];
[data release];
[super dealloc];
}
@end



import the XMLParser.h class in your class
Call the function parseDataWithCase for parsing xml
and then call the records it return a array of that xml.



XMLParser *xmlParser = [[XMLParser alloc] init];
[xmlParser parseDataWithCase];
myArray = [xmlParser records];
[xmlParser release];




(
{
name = "Amit";
age = 24;
},
{
name = "Akram";
age = 20;
},
{
name = "John";
age = 30;
},
)


You can customize your parse method as you own need


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

Thursday, April 22, 2010

Animated and transparent GIF’s for iPhone usign cocoa

Hi All

Show the animated/gif file in using cocoa in you Iphone application
Use this code

NSURL* firstUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@myfile" ofType:@"gif"]];
UIImageView * firstAnimation = [AnimatedGif getAnimationForGifAtUrl: firstUrl];

NSURL * secondUrl = [NSURL URLWithString:@url of animated image];
UIImageView * secondAnimation = [AnimatedGif getAnimationForGifAtUrl: secondUrl];

[theFirstAnimatedGif addSubview:firstAnimation];
[theSecondAnimatedGif addSubview:secondAnimation];

Use the code and give your comments


Thanks
Amit Battan

Friday, April 2, 2010

You & Me - Dots and Boxes (IPhone Application)

I have launch the next game 'You & Me - Boxes'


Its simple dot and boxes game.
You can play with your friend or device with easy and hard mode.
You can change the number of row and columns.
Just download and enjoy the game.

Free version is coming soon





Click here for our more IPhone Application



Thanks
Amit Battan

You & Me - Tic Tac Toe (Iphone/Ipad Application)

Me and Mandeep did release the you and me 'nought & cross' on the itunes stores.


Its a simple Tic-tac-toe, and alternatively called noughts and crosses, X's and O's, and many other names, is a pencil-and-paper game for two players, O and X, who take turns marking the spaces in a 3×3 grid, usually X going first. The player who succeeds in placing three respective marks in a horizontal, vertical or diagonal row wins the game.

This game has got a very simple and cool design which just remind us of the game which in our childhood we loved to play on paper.



Feature Include
  • Multiple Level
  • Sound functionality
  • 2 Player Mode
  • With Device
  • Customize game result according to level and type of game

Just enjoy the Game ..free version is coming soon

Click here for our more IPhone Application

Thanks
Amit Battan