IOS XML解析学习

 XML文件:

 
<?xml version="1.0" encoding="UTF-8"?>
<Notes>
<Note id="1">
<CDate>2013-6-11</CDate>
<Content>ksdjfaljdfasf</Content>
<UserID>ALEX</UserID>
</Note>
<Note id="2">
<CDate>2012-6-11</CDate>
<Content>VVVVVVVVVVV</Content>
<UserID>ALEX11</UserID>
</Note>
</Notes>
 

//

//  NotesXMLParser.h

//  XML

//

//  Created by Alex on 13-6-12.

//  Copyright (c) 2013 Alex. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface NotesXMLParser : NSObject<NSXMLParserDelegate>

@property (nonatomic,strongNSMutableArray *notes;

@property (nonatomicstrongNSString *currentTagName;

-(void)start;

@end

 

 

//

//  NotesXMLParser.m

//  XML

//

//  Created by Alex on 13-6-12.

//  Copyright (c) 2013 Alex. All rights reserved.

//

 

#import "NotesXMLParser.h"

 

@implementation NotesXMLParser

-(void)start

{

    NSString* path = [[NSBundle mainBundlepathForResource:@"notes" ofType:@"xml"];

    

    NSURL *url = [NSURL fileURLWithPath:path];

    //开始解析XML

    NSXMLParser *parser = [[NSXMLParser allocinitWithContentsOfURL:url];

    parser.delegate = self;

    [parser parse];

    NSLog(@"解析完成...");

}

 

//文档开始的时候触发

- (void)parserDidStartDocument:(NSXMLParser *)parser

{

    _notes = [NSMutableArray new];

}

 

//文档出错的时候触发

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError

{

    NSLog(@"%@",parseError);

}

 

//遇到一个开始标签时候触发

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName

  namespaceURI:(NSString *)namespaceURI

 qualifiedName:(NSString *)qualifiedName

    attributes:(NSDictionary *)attributeDict

{

    _currentTagName = elementName;

    if ([_currentTagName isEqualToString:@"Note"]) {

        NSString *_id = [attributeDict objectForKey:@"id"];

        NSMutableDictionary *dict = [NSMutableDictionary new];

        [dict setObject:_id forKey:@"id"];

        [_notes addObject:dict];

    }

    

}

 

//遇到字符串时候触发

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

    

     

string =[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //替换回车符和空格

    if ([string isEqualToString:@""]) {

        return;

    }

    NSMutableDictionary *dict = [_notes lastObject];

    

if ([_currentTagName isEqualToString:@"CDate"] && dict) {

        [dict setObject:string forKey:@"CDate"];

}

    

    if ([_currentTagName isEqualToString:@"Content"] && dict) {

        [dict setObject:string forKey:@"Content"];

}

    

    if ([_currentTagName isEqualToString:@"UserID"] && dict) {

        [dict setObject:string forKey:@"UserID"];

}

}

 

//遇到结束标签时候出发

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName

  namespaceURI:(NSString *)namespaceURI

 qualifiedName:(NSString *)qName;

{

    self.currentTagName = nil;

}

 

 

//遇到文档结束时候触发

- (void)parserDidEndDocument:(NSXMLParser *)parser

{

    [[NSNotificationCenter defaultCenterpostNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil];

    self.notes = nil;

}

@end

 
 

#import "ViewController.h"

#import "NotesXMLParser.h"

 

@interface ViewController ()

 

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

 

    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(reloadView:) name:@"reloadViewNotification" object:nil];

    

    NotesXMLParser *parser=[NotesXMLParser new];

    [parser start];

 

    

}

 

//本文转载http://www.dahuangphone.com/dv_rss.asp?s=xhtml&boardid=8&id=133&page=3

IOS XML解析学习,布布扣,bubuko.com

IOS XML解析学习

上一篇:Android:使用单元测试


下一篇:点酷听书手机客户端应用源码