// xml文件的样式 的样式有多种样式,标准的是把数据存放在子节点中,还有就是比较简单的读取就是把数据直接存在属性里面
#import
@interface RootViewController : UIViewController<</span>NSXMLParserDelegate>
#pragma mark - 声明--属性
@property (nonatomic, retain) NSMutableArray *messageeListArray; // 数据存储的地方
@property (nonatomic, retain) NSString *fromTagFlag; // 开始标签
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 获取xml字符串类型文件名
NSString *xmlDocPath = [[NSBundle mainBundle] pathForResource:@"document"ofType:@"xml"];
// 把字符串类型的网址转为NSURL类型
NSURL *url =[[NSURL alloc] initFileURLWithPath:xmlDocPath];
// 使用url实例化网络请求实例
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 同步读取到网络上的数据
NSData *urlData = [NSURLConnection sendSynchronousRequest:requestreturningResponse:nil error:nil];
// 实例化一个具体的解析类对象
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:urlData];
// 设置该解析类对象的代理
xmlParser.delegate = self;
// 调用方法开始解析xml文件
[xmlParser parse];
}
#pragma mark - NSXMLParser Delegate -
#pragma mark 文档开始读取
- (void)parserDidStartDocument:(NSXMLParser *)parser {
// 实例化存储数据的数组
self.messageeListArray = [NSMutableArray array];
}
#pragma mark 解析标签开始
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// 首先用属性fromTagFlag获取ElementName,供后面使用
self.fromTagFlag = elementName;
// 判断
if ([elementName isEqualToString:@"Message"]) {
NSString *identifier = [attributeDict objectForKey:@"id"];
NSMutableDictionary *oneDic = [NSMutableDictionary dictionary];
[oneDic setObject:identifier forKey:@"id"];
[self.messageeListArray addObject:oneDic];
}
}
#pragma mark 获取到标签对应的数据
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// 剔除获取的文本中空格和换行
string = [string stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
NSMutableDictionary *dict = [self.messageeListArray lastObject];
// 判断from标签
if ([_fromTagFlag isEqualToString:@"from"] && dict) {
[dict setObject:string forKey:@"from"];
}
// 判断CDate标签
if ([_fromTagFlag isEqualToString:@"CDate"] && dict) {
[dict setObject:string forKey:@"CDate"];
}
// 判断Content标签
if ([_fromTagFlag isEqualToString:@"Content"] && dict) {
[dict setObject:string forKey:@"Content"];
}
}
#pragma mark - 解析标签结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
self.fromTagFlag = nil;
}
#pragma mark 文档结束读取
- (void)parserDidEndDocument:(NSXMLParser *)parser {
// 打印获取到的数据的数组
NSLog(@"%@", self.messageeListArray);
}
#pragma mark - 重写----dealloc方法
- (void)dealloc {
[_messageeListArray release], _messageeListArray = nil;
[_fromTagFlag release], _fromTagFlag = nil;
// 调用父类的dealloc方法
[super dealloc];
}
@end
#import
@interface RootViewController : UIViewController<</span>NSXMLParserDelegate>
#pragma mark - 声明--属性
@property (nonatomic, retain) NSMutableArray *messageeListArray; // 数据存储的地方
@property (nonatomic, retain) NSString *fromTagFlag; // 开始标签
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 准备网址的字符串数据
NSString *urlString =@"http://flash.weather.com.cn/wmaps/xml/beijing.xml";
// 把字符串类型的网址转为NSURL类型
NSURL *url1 = [[NSURL alloc] initWithString:urlString];
// 使用url实例化网络请求实例
NSURLRequest *request = [NSURLRequest requestWithURL:url1];
// 同步读取到网络上的数据
NSData *urlData = [NSURLConnection sendSynchronousRequest:requestreturningResponse:nil error:nil];
// 实例化一个具体的解析类对象
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:urlData];
// 设置该解析类对象的代理
xmlParser.delegate = self;
// 调用方法开始解析xml文件
[xmlParser parse];
}
#pragma mark - NSXMLParser Delegate -
#pragma mark 文档开始读取
- (void)parserDidStartDocument:(NSXMLParser *)parser {
// 实例化存储数据的数组
self.messageeListArray = [NSMutableArray array];
}
#pragma mark 解析标签开始
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// 首先用属性fromTagFlag获取ElementName,供后面使用
self.fromTagFlag = elementName;
// 因为数据都是存储在属性中的,全都存储在了attributeDict里面了,在这里直接打印下
NSLog(@"%@", attributeDict);
}
#pragma mark 获取到标签对应的数据
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// 因为数据都存储在了属性中,所有不需要解析子标签
}
#pragma mark - 解析标签结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
self.fromTagFlag = nil;
}
#pragma mark 文档结束读取
- (void)parserDidEndDocument:(NSXMLParser *)parser {
// 打印获取到的数据的数组
NSLog(@"%@", self.messageeListArray);
}
#pragma mark - 重写----dealloc方法
- (void)dealloc {
[_messageeListArray release], _messageeListArray = nil;
[_fromTagFlag release], _fromTagFlag = nil;
// 调用父类的dealloc方法
[super dealloc];
}
@end