KissXML example
KissXML was inspired by the TouchXML project, but it was created to add full support for generating XML as well as supporting the entire NSXML API. KissXML is a good approach for parsing xml data, and the x-path functionallity makes it more powerful, you could also use NSXMLParser, it's not bad but it's very very slow and it uses lots of memory.
Creating Template Project
- Download source codes form here
- create a new View-Based Application project in xcode and name it: KissXML
- in your project create a new group, name it DDXML
- inside this group add NSStringAdditions.h, NSStringAdditions.m and all the files starting with DDXML (excluding DDXMLTesting.h and DDXMLTesting.m) at the end you got something like this:
- click Project -> Edit Project Settings.
- click build tab.
- add this to your compiler instructions Other Linker Flags = -lxml2 Header Search Paths = /usr/include/libxml2
Testing KissXML
add the sample XML file to your project, you can find it here
import DDXML in to kissXMLViewController.h
#import "DDXML.h"
now go to kissXMLViewController.m and replace all the code with this:
// // kissXMLViewController.m // kissXML // #import "kissXMLViewController.h" // Private methods @interface kissXMLViewController () -(void)parseXML:(NSString*)source; @end @implementation kissXMLViewController /* // The designated initializer. Override to perform setup that is required before the view is loaded. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization } return self; } */ /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; NSError *error; NSString *content = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bookstore" ofType:@"xml"] encoding:NSUTF8StringEncoding error:&error;]; [self parseXML:content]; } -(void)parseXML:(NSString*)source { NSError *error = nil; DDXMLDocument *theDocument = [[DDXMLDocument alloc] initWithXMLString:source options:0 error:&error;]; NSArray *results = [theDocument nodesForXPath:@"/bookstore/book[price>35]" error:&error;]; for (DDXMLElement *book in results) { NSLog(@"-----------"); NSString *category = [[book attributeForName:@"category"] stringValue]; NSLog(@"category:%@",category); for (int i = 0; i < [book childCount]; i++) { DDXMLNode *node = [book childAtIndex:i]; NSString *name = [node name]; NSString *value = [node stringValue]; NSLog(@"%@:%@",name,value); } } } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } @end
when you compile and run, you should see something like this:
[Session started at 2010-11-04 12:29:11 -0600.] 2010-11-04 12:29:12.418 kissXML[1619:207] ----------- 2010-11-04 12:29:12.420 kissXML[1619:207] category:WEB 2010-11-04 12:29:12.421 kissXML[1619:207] title:XQuery Kick Start 2010-11-04 12:29:12.422 kissXML[1619:207] author:James McGovern 2010-11-04 12:29:12.423 kissXML[1619:207] author:Per Bothner 2010-11-04 12:29:12.424 kissXML[1619:207] author:Kurt Cagle 2010-11-04 12:29:12.426 kissXML[1619:207] author:James Linn 2010-11-04 12:29:12.428 kissXML[1619:207] author:Vaidyanathan Nagarajan 2010-11-04 12:29:12.429 kissXML[1619:207] year:2003 2010-11-04 12:29:12.430 kissXML[1619:207] price:49.99 2010-11-04 12:29:12.432 kissXML[1619:207] ----------- 2010-11-04 12:29:12.432 kissXML[1619:207] category:WEB 2010-11-04 12:29:12.433 kissXML[1619:207] title:Learning XML 2010-11-04 12:29:12.433 kissXML[1619:207] author:Erik T. Ray 2010-11-04 12:29:12.434 kissXML[1619:207] year:2003 2010-11-04 12:29:12.435 kissXML[1619:207] price:39.95
as you can see, it's very simple setting up your project to get it work with KissXML if you want to learn more about x-path you can go to: http://www.w3schools.com/xpath/default.asp there are great tutororials about it.
No comments:
Post a Comment