Skip to main content

XML Parser



If XML inside bundle :

NSString *pathStr = [[[NSBundle mainBundleresourcePathstringByAppendingPathComponent:@"Mobile_amountDue.xml"]; 
    
    NSData *dataXml = [NSData dataWithContentsOfFile:pathStr];
    
url = [[NSURL allocinitWithString:pathStr];
    xmlParser = [[NSXMLParser allocinitWithData:dataXml];
    parser = [[XMLParser allocinitXMLParser:@"success"];
    [xmlParser setDelegate:parser];
    [xmlParser parse];
if([newsArray count]>0)
{
[newsArray removeAllObjects]; 
}
    newsArray=[parser.parsedXML mutableCopy];


XML read from URL :

#define URL_STRING @"http://www.espncricinfo.com/rss/content/story/feeds/0.xml"// RSS url
#define OUTER_TAG @"item" //Parent Tag


url = [[NSURL allocinitWithString:URL_STRING];
    xmlParser = [[NSXMLParser allocinitWithContentsOfURL:url];
    parser = [[XMLParser allocinitXMLParser:OUTER_TAG];
    [xmlParser setDelegate:parser];
    [xmlParser parse];
if([newsArray count]>0)
{
[newsArray removeAllObjects]; 
}
 newsArray=[parser.parsedXML mutableCopy];


////////////////////////////////////////////XMLParser.h/////////////////////////////////////
#import 

@interface XMLParser : NSObject <NSXMLParserDelegate>{

NSMutableString *currentElementValue;
NSString *repetingTag;
NSMutableArray *parsedXML;
NSMutableDictionary *curObj;
}
@property (nonatomicretainNSMutableArray *parsedXML;

- (XMLParser *) initXMLParser :(NSString *)repetingTagStr ;

@end



////////////////////////////////////////////XMLParser.m/////////////////////////////////////


#import "XMLParser.h"

@implementation XMLParser
@synthesize parsedXML;
- (XMLParser *) initXMLParser :(NSString *)repetingTagStr{
[super init];
parsedXML = [[NSMutableArray allocinit];
repetingTag=repetingTagStr;
return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict {
currentElementValue = nil;
if([elementName isEqualToString:repetingTag]) {
curObj=[[NSMutableDictionary alloc]init];
}
for (NSString *key in attributeDict) {
NSString *vKeyValue = [attributeDict valueForKey:key];
[curObj setValue:[vKeyValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:key];
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
if(!currentElementValue
currentElementValue = [[NSMutableString allocinitWithString:string];
else
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//There is nothing to do if we encounter the tourneys element here.
//If we encounter the tourneys element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:repetingTag]) {
[parsedXML addObject:curObj];
//NSLog(@"cdsvdvhdshvdov;===%@",parsedXML);
[curObj release];
curObj = nil;
}
else 
{

[curObj setValue:[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSString *errorMessage;
if([parseError code]==5)
{
errorMessage=@"Internet connection not found. Please ensure connectivity for the app to work correctly.";

UIAlertView *alert = [[UIAlertView alloc
  initWithTitle:@"No Network" 
  message:@"A network connection is required.  Please verify your network settings and try again." 
  delegate:nil cancelButtonTitle:nil 
  otherButtonTitles:@"OK"nil];
[alert show];
    [alert release];
}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser{

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

@end

Comments

Popular posts from this blog

Symbolicating Crash Logs

Link to download symbolicatecrash file https://github.com/chrispix/ symbolicatecrash-fix 1. Using XCode:  This is probably the easiest way to symbolicate the crash reports, but not always effective. To symbolicate using XCode you need three files: a.     Crash report (.crash file). b.     Symbol file (.dSYMB file). c.     Application bundle (.app file). In most of the cases you will have “yourapp.ipa” file, to extract “yourapp.app“ from “yourapp.ipa” just change the extension of “yourapp.ipa” to “yourapp.zip” and extract the zip file, you will get a folder named “Payload”, in this folder you will have “yourapp.app” package. 2. Using symbolicatecrash command: Some times XCode do not symbolicate the crash log properly, In this situation we can use symbolicatecrash script manually. Before proceeding keep you “.app”,  ”.dSYM” and “.crash” files in one folder. Now open ...

iOS Beta Builder

Download iOS Beta Builder 1.0 While I’ve been working on iOS for awhile, I don’t have a ton of experience with Cocoa proper – I’m sure there are probably some issues that slipped through the testing process. Screenshots:   How Does It Work? 1. Build your .ipa file using Xcode’s ‘Build and Archive’ option. Choose ‘Save to Disk’. 2. Launch BetaBuilder (or drag and drop the .ipa on to it). If you need to, select your .ipa file. The app should pre-fill the other details. 3. Enter your intended deployment URL. This is the URL on the Web where your beta will be posted / viewed in a browser. This info gets baked into the deployment file. 4. Hit ‘Generate Deployment Files’ and pick a location to output the files. That’s it – just upload the generated files to your Web server and then hit the URL in the device’s browser. BetaBuilder simply generates the HTML and manifest files needed to make wireless distribution work. As a convenience, it also bundles an iTunes install...

Convert Date into ISO formate

Call below function like : [self toStringFromDateTime:[NSDate date]]; - (NSString*)toStringFromDateTime:(NSDate*)datetime {     // Purpose: Return a string of the specified date-time in UTC (Zulu) time zone in ISO 8601 format.     // Example: 2013-10-25T06:59:43.431Z     NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];     //ISO DateFormatter     [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@" UTC "]];     [dateFormatter setDateFormat:@" yyyy-MM-dd'T'HH:mm:SS.SSS'Z' "];     NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];     return dateTimeInIsoFormatForZuluTimeZone; }