标签:
xml解析主要可以使用CData,libxml2以及NSXMLParser,以下对各个方法给出了相应的例子:
1.CDataXML:
1.1.创建FKBook类
#import <Foundation/Foundation.h> @interface FKBook : NSObject @property (nonatomic , assign) NSInteger bookID; @property (nonatomic , copy) NSString *title; @property (nonatomic , copy) NSString *author; @property (nonatomic , copy) NSString *remark; @end
#import "FKBook.h"
@implementation FKBook
@synthesize bookID,title,author,remark;
@end
1.2.创建 FKBooksParser 类
#import <Foundation/Foundation.h> @interface FKBooksParser : NSObject - (NSArray*) parseXML:(NSString*) xmlName; @end
#import "FKBooksParser.h" #import "FKBook.h" #import "GDataXMLNode.h" @implementation FKBooksParser - (NSArray*) parseXML:(NSString*) xmlName { // 使用NSBundle对象获取到需要解析的XML文档的路径 NSString *path = [[NSBundle mainBundle] pathForResource:xmlName ofType:@"xml"]; // 使用NSFileHandle对象根据文件路径获取到文件 NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path]; // 读取文件内容返回NSData对象 NSData *data = [file readDataToEndOfFile]; // 根据NSData对象初始化GDataXMLDocument对象 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; // 如果需要根据XML字符串来初始化GDataXMLDocument对象,则调用如下代码 // GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlStr // options:0 error:nil]; // 获取根元素,也就是获取<books.../>元素 GDataXMLElement *rootElement = [doc rootElement]; // 获取rootElement下所有<book.../>元素,返回所有<book.../>元素组成的集合 NSArray *bookElements = [rootElement elementsForName:@"book"]; // 初始化一个可变数组,用于存储将要获取的所有<book.../>元素的内容 NSMutableArray *books = [[NSMutableArray alloc] init]; // 循环遍历每一个<book.../>元素 for(GDataXMLElement *bookElement in bookElements) { // 初始化FKBook对象 FKBook *book = [[FKBook alloc] init]; // 获取id属性值,并且转成整形 NSInteger bookID = [[[bookElement attributeForName:@"id"] stringValue] integerValue]; // 获取title,author,remark元素内容 NSString *title = [[[bookElement elementsForName:@"title"] objectAtIndex:0] stringValue]; NSString *author = [[[bookElement elementsForName:@"author"] objectAtIndex:0] stringValue]; NSString *remark = [[[bookElement elementsForName:@"remark"] objectAtIndex:0] stringValue]; // 将获取的属性值和元素内容存储到FKBook对象的属性中 book.bookID = bookID; book.title = title; book.author = author; book.remark = remark; //将每一个Book对象添加到可变数组 [books addObject:book]; } //返回books集合的副本 return [books copy]; } @end
1.3.创建FKViewController : UITableViewController
#import "FKViewController.h" #import "FKBooksParser.h" #import "FKBook.h" @implementation FKViewController NSArray* books; - (void)viewDidLoad { [super viewDidLoad]; // 获取FKBooksParser实例对象 FKBooksParser* booksParser = [[FKBooksParser alloc] init]; // 解析XML文档,获取解析得到的NSArray集合 books =[booksParser parseXML:@"books"]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // books属性包含多少个元素,此处就显示多少个表格行。 return books.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 获取可重用的单元格 UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: @"bookCell" forIndexPath:indexPath]; // 从可重用单元格中根据Tag分别取出3个UILabel控件 UILabel* titleLabel = (UILabel*)[cell viewWithTag:1]; UILabel* authorLabel = (UILabel*)[cell viewWithTag:2]; UILabel* remarkLabel = (UILabel*)[cell viewWithTag:3]; FKBook* book = [books objectAtIndex:indexPath.row]; // 为3个UILabel设置文本 titleLabel.text = book.title; authorLabel.text = book.author; remarkLabel.text = book.remark; return cell; } @end
2.libxml2XML:
2.1.创建FKBook类 #import <Foundation/Foundation.h> @interface FKBook : NSObject @property (nonatomic , assign) NSInteger bookID; @property (nonatomic , copy) NSString *title; @property (nonatomic , copy) NSString *author; @property (nonatomic , copy) NSString *remark; @end #import "FKBook.h" @implementation FKBook @synthesize bookID,title,author,remark; @end 2.2.创建 FKBooksParser 类 #import <Foundation/Foundation.h> @interface FKBooksParser : NSObject - (NSArray*) parseXML:(NSString*) xmlName; @end #import "FKBooksParser.h" #import "FKBook.h" #import "GDataXMLNode.h" @implementation FKBooksParser - (NSArray*) parseXML:(NSString*) xmlName { // 使用NSBundle对象获取到需要解析的XML文档的路径 NSString *path = [[NSBundle mainBundle] pathForResource:xmlName ofType:@"xml"]; // 使用NSFileHandle对象根据文件路径获取到文件 NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path]; // 读取文件内容返回NSData对象 NSData *data = [file readDataToEndOfFile]; // 根据NSData对象初始化GDataXMLDocument对象 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; // 如果需要根据XML字符串来初始化GDataXMLDocument对象,则调用如下代码 // GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlStr // options:0 error:nil]; // 获取根元素,也就是获取<books.../>元素 GDataXMLElement *rootElement = [doc rootElement]; // 获取rootElement下所有<book.../>元素,返回所有<book.../>元素组成的集合 NSArray *bookElements = [rootElement elementsForName:@"book"]; // 初始化一个可变数组,用于存储将要获取的所有<book.../>元素的内容 NSMutableArray *books = [[NSMutableArray alloc] init]; // 循环遍历每一个<book.../>元素 for(GDataXMLElement *bookElement in bookElements) { // 初始化FKBook对象 FKBook *book = [[FKBook alloc] init]; // 获取id属性值,并且转成整形 NSInteger bookID = [[[bookElement attributeForName:@"id"] stringValue] integerValue]; // 获取title,author,remark元素内容 NSString *title = [[[bookElement elementsForName:@"title"] objectAtIndex:0] stringValue]; NSString *author = [[[bookElement elementsForName:@"author"] objectAtIndex:0] stringValue]; NSString *remark = [[[bookElement elementsForName:@"remark"] objectAtIndex:0] stringValue]; // 将获取的属性值和元素内容存储到FKBook对象的属性中 book.bookID = bookID; book.title = title; book.author = author; book.remark = remark; //将每一个Book对象添加到可变数组 [books addObject:book]; } //返回books集合的副本 return [books copy]; } @end
2.3创建FKViewController : UITableViewController
#import "FKViewController.h" #import "FKBooksParser.h" #import "FKBook.h" @implementation FKViewController FKBooksParser* booksParser; - (void)viewDidLoad { [super viewDidLoad]; // 获取FKBooksParser实例对象 booksParser = [[FKBooksParser alloc] init]; // 解析XML文档 [booksParser readXml:@"books"]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // booksParser的books属性包含多少个元素,此处就显示多少个表格行。 return booksParser.books.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 获取可重用的单元格 UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: @"bookCell" forIndexPath:indexPath]; // 从可重用单元格中根据Tag分别取出3个UILabel控件 UILabel* titleLabel = (UILabel*)[cell viewWithTag:1]; UILabel* authorLabel = (UILabel*)[cell viewWithTag:2]; UILabel* remarkLabel = (UILabel*)[cell viewWithTag:3]; FKBook* book = [booksParser.books objectAtIndex:indexPath.row]; // 为3个UILabel设置文本 titleLabel.text = book.title; authorLabel.text = book.author; remarkLabel.text = book.remark; NSLog(@"---%d" , book.bookID); return cell; } @end
3.NSXMLParser
#import <Foundation/Foundation.h> @interface FKXMLPraserDelegate : NSObject <NSXMLParserDelegate> // 定义一个NSMutableArray集合来保存解析XML文档得到的数据 @property (nonatomic, strong) NSMutableArray *books; @end #import "FKXMLPraserDelegate.h" #import "FKBook.h" @implementation FKXMLPraserDelegate // 定义一个FKBook对象,用于保存正在解析的<book>元素中的数据 FKBook* book; NSString* currentElementValue; // 当开始处理某个元素时触发该方法 -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { NSLog(@"开始处理元素 %@" , elementName); if([elementName isEqualToString:@"books"]) { // 如果正在处理根元素,在此处初始化存储解析结果的NSMutableArray集合 self.books = [[NSMutableArray alloc] init]; } // 如果正在处理<book.../>元素 else if([elementName isEqualToString:@"book"]) { // 初始化FKBook对象 book = [[FKBook alloc] init]; // 从attributeDict中读取<book.../>元素的属性id的值 book.bookID = [[attributeDict objectForKey:@"id"] integerValue]; } } // 当开始处理字符串内容时触发该方法 -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { NSLog(@"处理字符串内容:%@",string); // 如果当前的字符串值不为nil,保存当前正在处理的元素的值 if(string) { currentElementValue = string; } } // 当处理某个元素结束时触发该方法 -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { // 如果处理根元素结束,表明XML文档处理完成。 if([elementName isEqualToString:@"books"]) { return; } // 如果处理<book.../>元素结束,则将封装的FKBook对象添加到NSMutableArray集合中 else if([elementName isEqualToString:@"book"]) { [self.books addObject: book]; book = nil; } else { // 如果既不是处理<books.../>元素,也不是处理<book.../>元素的时候, // 则使用KVC方式为当前FKBook对象的属性赋值 [book setValue:currentElementValue forKey:elementName]; currentElementValue = nil; } } @end
标签:
原文地址:http://my.oschina.net/khakiloveyty/blog/396671