XML文件有多种样式,标准的是把数据存在字节点上,还有的就是把数据直接存储在属性里面
#import"ViewController.h"
@interface ViewController ()<NSXMLParserDelegate>{
NSMutableArray * listArr;
NSString *elementTag;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1----获取工程中的资源
NSString * path = [[ NSBundle mainBundle] pathForResource:@"student" ofType:@"xml"];
// NSLog(@"%@",path);
//2----获取资源文件保存数据
NSData * data = [NSData dataWithContentsOfFile:path];
// NSLog(@"%@",data);
//3.---指定解析对象
NSXMLParser * parper = [[NSXMLParser alloc] initWithData:data];
parper.delegate = self;
NSLog(@"%@",parper);
//4.-- --开始解析
[parper parse];
}
//5.----开始解析准备工作---(从头标签开始)
-(void)parserDidStartDocument:(NSXMLParser *)parser{
//一般在此方法进行数组或字典等对象的初始化
listArr = [NSMutableArray array];
}
//6.-----开始解析头标签
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//elementName 头标签名。attributeDict代表头标签所在<>中 用=连接的内容
elementTag = elementName;
if ([elementName isEqualToString:@"User"]) {
NSString * s = [attributeDict objectForKey:@"id"];
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[dict setObject:s forKey:@"id"];
[listArr addObject:dict];
}
}
//7.------获取标签存储的字符串
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSMutableDictionary * dic = [listArr lastObject];
if ([elementTag isEqualToString:@"name"] || [elementTag isEqualToString:@"age"]) {
[dic setObject:string forKey:elementTag];
}
}
//8.-----将elementTag置空
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//解析头标签结束之后,将elementTag置空,避免尾标签对foundCharacters造成的影响
elementTag = nil;
}
//9.----准备结束解析工作---(在尾标签结束)-(void)parserDidEndDocument:(NSXMLParser *)parser{
for (NSMutableDictionary * dic in listArr) {
NSLog(@"-->%@,%@,%@",[dic objectForKey:@"id"],dic[@"name"],dic[@"age"]);
}}
===================================================
JSON解析
1.先取出路径
2.获取资源文件保存数据
3.用系统自带库进行解析--(致此解析完成)
4.取出:a.先取出最外层的数组或者字典 , b,然后一次往里面解析数组或者字典 c. 最后对数组或者字典进行遍历
Weather.h
#import <Foundation/Foundation.h>
@interface Weather : NSObject
@property (nonatomic,copy)NSString * raRadarde;
@property (nonatomic,copy)NSString * SD;
@property (nonatomic,copy)NSString * WD;
@property (nonatomic,copy)NSString * WS;
@property (nonatomic,copy)NSString * WSE;
@property (nonatomic,copy)NSString * city;
@property (nonatomic,copy)NSString * cityid;
@property (nonatomic,copy)NSString * isRadar;
@property (nonatomic,copy)NSString * njd;
@property (nonatomic,copy)NSString * qy;
@property (nonatomic,copy)NSString * temp;
@property (nonatomic,copy)NSString * time;
@interface Weather : NSObject
@property (nonatomic,copy)NSString * raRadarde;
@property (nonatomic,copy)NSString * SD;
@property (nonatomic,copy)NSString * WD;
@property (nonatomic,copy)NSString * WS;
@property (nonatomic,copy)NSString * WSE;
@property (nonatomic,copy)NSString * city;
@property (nonatomic,copy)NSString * cityid;
@property (nonatomic,copy)NSString * isRadar;
@property (nonatomic,copy)NSString * njd;
@property (nonatomic,copy)NSString * qy;
@property (nonatomic,copy)NSString * temp;
@property (nonatomic,copy)NSString * time;
@end
Weather.m
#import "Weather.h"
@implementation Weather
-(NSString *)description{
return [NSString stringWithFormat:@"%@:%@ ,%@,%@ ,%@,%@,%@:%@ ,%@,%@,%@,%@",_raRadarde,_SD,_WD,_WS,_WSE,_city,_cityid,_isRadar,_njd,_qy,_temp,_time];
}
@end
@implementation Weather
-(NSString *)description{
return [NSString stringWithFormat:@"%@:%@ ,%@,%@ ,%@,%@,%@:%@ ,%@,%@,%@,%@",_raRadarde,_SD,_WD,_WS,_WSE,_city,_cityid,_isRadar,_njd,_qy,_temp,_time];
}
@end
ViewControl.h
#import "ViewController.h"
#import "Weather.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * str = @"http://www.weather.com.cn/data/sk/101010100.html";
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
// NSLog(@"%@",data);
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@",obj);
NSDictionary *dataDic = [obj objectForKey:@"weatherinfo"];//取出最外层的字典
NSLog(@"%@",dataDic);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "Weather.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * str = @"http://www.weather.com.cn/data/sk/101010100.html";
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
// NSLog(@"%@",data);
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@",obj);
NSDictionary *dataDic = [obj objectForKey:@"weatherinfo"];//取出最外层的字典
NSLog(@"%@",dataDic);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//网络数据解析分为两种方式(XML,JSON),其中JSON使我们常用的比较简单的解析方式
//JSON--解析步骤:
//1--先取出文件路径
//2--获取资源数据保存
//3--用系统库自带的方式(NSJSONSerialization)进行解析,(致此解析完成)
//--取出解析结果:a.先取出最外层的数组或者字典 ,b.然后依次往里面解析数组或者字典 c.最后对需要的数组或者字典进行遍历
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取工程中的资源
NSString * path = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"json"];
//将资源文件转换为data数据
NSData * data = [NSData dataWithContentsOfFile:path];
//JSON--解析步骤:
//1--先取出文件路径
//2--获取资源数据保存
//3--用系统库自带的方式(NSJSONSerialization)进行解析,(致此解析完成)
//--取出解析结果:a.先取出最外层的数组或者字典 ,b.然后依次往里面解析数组或者字典 c.最后对需要的数组或者字典进行遍历
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取工程中的资源
NSString * path = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"json"];
//将资源文件转换为data数据
NSData * data = [NSData dataWithContentsOfFile:path];
//用系统自带库进行解析(只能对数据进行解析)
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@",obj);
//取出url
NSArray * arr = [obj objectForKey:@"student"];
NSLog(@"%@",arr);
for (NSMutableDictionary * dict in arr) {
// NSLog(@"%@",dict);
NSString * str = dict[@"name"];
NSLog(@"%@",str);
NSString * str1 = dict[@"age"];
NSLog(@"%@",str1);
NSString * str2 = dict[@"scroe"];
NSLog(@"%@",str2);
// NSLog(@"-->%@,%@,%@",dict[@"name"],dict[@"age"],dict[@"scroe"]);
}
// NSLog(@"%@",obj);
//取出url
NSArray * arr = [obj objectForKey:@"student"];
NSLog(@"%@",arr);
for (NSMutableDictionary * dict in arr) {
// NSLog(@"%@",dict);
NSString * str = dict[@"name"];
NSLog(@"%@",str);
NSString * str1 = dict[@"age"];
NSLog(@"%@",str1);
NSString * str2 = dict[@"scroe"];
NSLog(@"%@",str2);
// NSLog(@"-->%@,%@,%@",dict[@"name"],dict[@"age"],dict[@"scroe"]);
}
}