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

iOS9 iTune Upload Error : Invalid Bundle. iPad Multitasking support requires launch story board in bundle

1. Add the UIRequiresFullScreen key to your Xcode project’s Info.plist file and apply the Boolean value YES 2. This will fix one more issue that is coming at the time of iTune upload Invalid Bundle. iPad Multitasking support requires these orientations: 'UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight'. If your application only using single orientation then use the above keyword in info.plist

XCConfig files are not properly deintegrated

After looking into the different block and searched over the internet, still not able to find a valid reason for this error. I also tried "pod deintegrate" but that will also not worked for me. After installing pods, I got a warning message(pasted below). It seems, this is the reason for XCConfig error. This may be because my team member using older version of CocoaPods.. [!] The version of CocoaPods used to generate the lockfile (1.6.1) is higher than the version of the current executable (1.5.2). Incompatibility issues may arise. To fix this issue I follow the below steps, maybe this will work for you. 1. Right click on xxx.xcodeproj file and click on "Show Package Contents" 2. Open "project.pbxproj" file in edit mode 3. Search " Pods; " in opened file 4. Change : path = Pods; to name = Pods; NOTE:  "path" for the newer version and "name" for the older version. Please let me know if it works for you or...