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,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
FKViewController.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,copy) NSString *remark; @end #import "FKBook.h" @implementation FKBook @synthesize bookID,remark; @end 2.2.创建 FKBooksParser 类 #import <Foundation/Foundation.h> @interface FKBooksParser : NSObject - (NSArray*) parseXML:(NSString*) xmlName; @end #import " 2.3创建FKViewController : UITableViewController
FKBook.h" @implementation FKViewController FKBooksParser* booksParser; - (// 获取FKBooksParser实例对象 booksParser = [[FKBooksParser alloc] init]; // 解析XML文档 [booksParser readXml:@"// booksParser的books属性包含多少个元素,此处就显示多少个表格行。 return booksParser.books.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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); 3.NSXMLParser