XML锌杀、JSON解析

XML解析

#import "RootViewController.h"
#import "StudentModel.h"

@interface RootViewController ()<NSXMLParserDelegate>

@property(nonatomic,strong)UIButton *saxbutton;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableString *myString;

@end

@implementation RootViewController

// 解析

// iOS常見的兩種數(shù)據(jù)格式 : XML 和 JSON

// XML 解析方法 1. (系統(tǒng)方法)SAX解析  2. (google) GData方法

// JSON 1. 系統(tǒng)方法 2. JSONKit


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.saxbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.saxbutton.frame = CGRectMake(100, 100, 100, 100);
    self.saxbutton.backgroundColor = [UIColor blueColor];
    [self.saxbutton setTitle:@"sax解析" forState:UIControlStateNormal];
    self.saxbutton.tintColor = [UIColor redColor];
    [self.view addSubview:self.saxbutton];
   
    [self.saxbutton addTarget:self action:@selector(saxbuttonAction:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)saxbuttonAction:(UIButton *)sender{
    [self p_data];
}
// 處理數(shù)據(jù)的私有方法
- (void)p_data{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"txt"];
    // 先把數(shù)據(jù)從文件中讀出來
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSLog(@"%@",data);
   
    // 1. 系統(tǒng)解析類
    NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data];
    // 2. 設(shè)置代理
    xmlParser.delegate = self;
    // 3. 開始解析
    [xmlParser parse];  
}
#pragma mark 代理方法
// 1. 開始解析文檔
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"開始解析文件");
    self.dataArr = [NSMutableArray array];
}
// 2. 開始解析標(biāo)簽
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"student"]) {
        StudentModel *s = [[StudentModel alloc]init];
        // 判斷標(biāo)簽名,如果是的話,就放到數(shù)組里面
        [self.dataArr addObject:s];
    }
}
// 3. 解析內(nèi)容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    // 把內(nèi)容拿到自己的字符串中
    self.myString = [NSMutableString string];
    [self.myString appendString:string];
}
// 4. 結(jié)束解析標(biāo)簽
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    StudentModel *s = [self.dataArr lastObject];
    [s setValue:self.myString forKey:elementName];
}
// 5. 結(jié)束解析文檔
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    for (StudentModel *s in self.dataArr) {
        NSLog(@"name:%@ gender:%@ hobby:%@ word:%@",s.name,s.gender,s.hobby,s.word);
    }
}

XML_GData解析

#import "RootViewController.h"
#import "Person.h"
#import "GDataXMLNode.h"

@interface RootViewController ()

@property(nonatomic,strong)UIButton *domButton;
@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.domButton = [UIButton buttonWithType:UIButtonTypeSystem];
   
    self.domButton.frame = CGRectMake(100, 100, 100, 100);
   
    [self.domButton setTitle:@"dom解析" forState:UIControlStateNormal];
   
    [self.view addSubview:self.domButton];
   
    [self.domButton addTarget:self action:@selector(domButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    // Do any additional setup after loading the view.
}

- (void)domButtonAction:(UIButton *)sender{
    [self p_Gdate];
}
// 這里我們使用谷歌提供好的第三方,進(jìn)行XML解析,這個第三方通過很好的封裝,解析更穩(wěn)定,更快
- (void)p_Gdate{
    NSLog(@"GDate解析");
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    self.dataArr = [NSMutableArray array];
   
    // 1. 初始化一個XMLDocument
    GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
   
    // 2. 生成根節(jié)點
    GDataXMLElement *rootElement = xmlDocument.rootElement;
    // 通過根標(biāo)簽 拿到子標(biāo)簽
    for (GDataXMLElement *childElement in rootElement.children) {
        Person *person = [[Person alloc]init];
        // stringvalue的屬性可以拿到標(biāo)簽下面的值
        // name的屬性,可以拿到標(biāo)簽的名字
        for (GDataXMLElement *valueElement in childElement.children) {
            [person setValue: valueElement.stringValue forKey:valueElement.name];
        }
        [self.dataArr addObject:person];
    }
    for (Person *per in self.dataArr) {
        NSLog(@"name:%@ gender:%@ hobby: %@ word: %@",per.name,per.gender,per.hobby,per.word);
    }
}

JSON解析

#import "RootViewController.h"
#import "Student.h"
#import "JSONKit.h"

@interface RootViewController ()

@property(nonatomic,strong)UIButton *jsonButton;

@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.jsonButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.jsonButton.frame = CGRectMake(100, 100, 100, 100);
    [self.jsonButton setTitle:@"JSON解析" forState:UIControlStateNormal];
    [self.view addSubview:self.jsonButton];
   
    [self.jsonButton addTarget:self action:@selector(jsonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    // Do any additional setup after loading the view.
}
- (void)jsonButtonAction:(UIButton *)sender{
    [self p_JSONdate];
}
- (void)p_JSONdate{
    // 使用系統(tǒng)方法
    /*
    NSLog(@"JSON解析");
    // 1. 獲取文件路徑
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data_json" ofType:@"txt"];
   
    // 2. 保存data
    NSData *data = [NSData dataWithContentsOfFile:filePath];
   
    // 3. 使用系統(tǒng)的方法進(jìn)行json解析
   NSArray *tempArr =   [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    self.dataArr = [NSMutableArray array];
    // 4. forin 利用KVC進(jìn)行賦值
    for (NSDictionary *d in tempArr) {
        Student *s = [[Student alloc]init];
        [s setValuesForKeysWithDictionary:d];
        [self.dataArr addObject:s];
    }
    for (Student *s in self.dataArr) {
        NSLog(@"name: %@ gender: %@ hobby: %@ word: %@",s.name,s.gender,s.hobby,s.word);
    }
  */
    //  使用第三方方法 JSONKit
    // 1. 獲取文件路徑
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data_json" ofType:@"txt"];
    // 2. 保存data
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    self.dataArr = [NSMutableArray array];
    self.dataArr = [data objectFromJSONData];

    for (NSDictionary *s in self.dataArr) {
        Student *stu = [[Student alloc]init];
        [stu setValuesForKeysWithDictionary:s];
        NSLog(@"name:%@ gender: %@ hobby: %@ word: %@",stu.name,stu.gender,stu.hobby,stu.word);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末殴蹄,一起剝皮案震驚了整個濱河市袭灯,隨后出現(xiàn)的幾起案子绑嘹,更是在濱河造成了極大的恐慌,老刑警劉巖工腋,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件擅腰,死亡現(xiàn)場離奇詭異,居然都是意外死亡惕鼓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門矾飞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呀邢,“玉大人,你說我怎么就攤上這事价淌。” “怎么了括尸?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵病毡,是天一觀的道長。 經(jīng)常有香客問我有送,道長,這世上最難降的妖魔是什么雀摘? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮涯塔,結(jié)果婚禮上豌注,老公的妹妹穿的比我還像新娘灯萍。我一直安慰自己,他們只是感情好旦棉,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著救斑,像睡著了一般真屯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上绑蔫,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機與錄音携添,去河邊找鬼篓叶。 笑死,一個胖子當(dāng)著我的面吹牛缸托,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播母谎,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼京革,長吁一口氣:“原來是場噩夢啊……” “哼幸斥!你這毒婦竟也來了咬扇?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤经窖,失蹤者是張志新(化名)和其女友劉穎梭灿,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體堡妒,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年搬泥,在試婚紗的時候發(fā)現(xiàn)自己被綠了忿檩。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡燥透,死狀恐怖辨图,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情徒役,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布杉女,位于F島的核電站鸳吸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏晌砾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一哼勇、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧积担,春花似錦、人聲如沸先誉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽渴庆。三九已至,卻和暖如春把曼,著一層夾襖步出監(jiān)牢的瞬間漓穿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工晃危, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人震叮。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓鳍鸵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親偿乖。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容