XML Parsar
Xml Example :
Hi Friends !!! Here I am Trying My Best To explain how Use Xml In IOS. So,I think its may be very helpful to you, Because here I am Trying so simple & easy language and method so that everyone can understand it very quickly.
Here xml Example using the GdataXml parser..
1. First put the xml folder in your new project ...(that given below) in this Folder include the AsiHttp ,GdataXml ,Rss ,Singleton Folders.
2. xml folder contain all the non ARC file so set the flag (-fno-objc-arc ) in compile sources.
(go to Build Phases --> compile sources ---> set flag to -fno-objc-arc)
-security.framework
-systemConfiguratin.framework
-libz.dylib
-MobileCoreServices.framework
-CFNetwork.framework
-libxml2.dylib
4. inser new path in header searchpath.(go Build Setting -> Search path-->Header Search Paths)
/usr/include/libxml2
and finally build your project..(cmd +b). (if error occur than see and check again all step).
so no error occur than ...
your viewcontroller.h
// ViewController.h// xml demo// Created by Ilesh panchal on #import <UIKit/UIKit.h>@interface ViewController : UIViewController{ NSMutableArray *allEntries; NSOperationQueue *queue; NSMutableArray *arrData;}@property (nonatomic,strong) NSMutableArray *allEntries;@property (nonatomic,strong) NSOperationQueue *queue;@property (nonatomic,strong) NSMutableArray *arrData;
@end
and view controller .m flle put the following method..
--- Import following files
#import "ASIHTTPRequest.h"#import "GDataXMLNode.h"#import "GDataXMLElement-Extras.h"#import "NSDate+InternetDateTime.h"#import "NSArray+Extras.h"#import "Singleton.h"
In viewDidLoad Method put this file in that..
arrData=[[NSMutableArray alloc] init]; self.allEntries = [NSMutableArray array]; self.queue = [[NSOperationQueue alloc] init]; // changge your url in this.. NSURL *url = [NSURL URLWithString:@"http://www.sandesh.com/cms/xml/Ahmedabad%20District.xml"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [_queue addOperation:request];
copy the following method ..
- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries { if ([rootElement.name compare:@"rss"] == NSOrderedSame) { [self parseRss:rootElement entries:entries]; } else { NSLog(@"Unsupported root element: %@", rootElement.name); }
}
- (void)requestFinished:(ASIHTTPRequest *)request { [_queue addOperationWithBlock:^{ NSError *error; GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:[request responseData] options:0 error:&error]; if (doc == nil) { NSLog(@"Failed to parse %@", request.url); } else { NSMutableArray *entries = [NSMutableArray array]; [self parseFeed:doc.rootElement entries:entries]; // [[NSOperationQueue mainQueue] addOperationWithBlock:^{ }]; } }];}- (void)requestFailed:(ASIHTTPRequest *)request { NSError *error = [request error]; NSLog(@"Error: %@", error); }
and finally add the following method and modify your tag is different ..
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries{ //here my first tag in the rss fied ... NSArray *channels = [rootElement elementsForName:@"channel"]; // NSLog(@"%@",channels); for (GDataXMLElement *channel in channels) { NSArray *items = [channel elementsForName:@"item"]; //NSLog(@"%@",items); for (GDataXMLElement *item in items) { NSString *articleTitle = [item valueForChild:@"title"]; NSLog(@"%@",articleTitle); // NSString *strDes = [item valueForChild:@"description"]; // NSString *strLink =[item valueForChild:@"link"];// NSString *articleDateString = [item valueForChild:@"pubDate"]; NSString *strcity = [(GDataXMLElement *) [locations attributeForName:@"city"] stringValue]; // if data is in Custom tag then Get data using This Coding :-[ <yweather:location city="Rudersdorf" region="OO" country="Austria"/> ] } }} |

No comments:
Post a Comment