XML解析1_XMLParser

前端之家收集整理的这篇文章主要介绍了XML解析1_XMLParser前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#import "ViewController.h"
#import "TBXML.h"

@interface ViewController () <NSXMLParserDelegate>

@property (nonatomic,strong) NSMutableArray *mulArray;
@property (nonatomic,strong) NSMutableDictionary *mulDict;
@property (nonatomic,copy) NSString *key;
@property (nonatomic,copy) NSString *startTag;

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [self saxXML];
}

- (void)saxXML {
    
    // 1.
    //   NSString *path =  [[NSBundle mainBundle] pathForResource:@"Notes" ofType:@"xml"];
    //    NSURL *url = [NSURL fileURLWithPath:path];
    // 2.
    // ----------创建对象
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Notes" withExtension:@"xml"];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    // -----------设置委托
    parser.delegate = self;
    // -----------开始解析
    [parser parse];
}

// <Note id="1">
// <CDate>2012-12-21</CDate>
// <Content>早上8点钟到公司</Content>
// <UserID>Allen</UserID>
// </Note>
#pragma mark -
#pragma mark  xml parser delegate methods 5个方法 1. 文件解析开始 2. 开始标签 3. 标签内容 4. 结束标签 5. 文件解析结束

/** 1. 文档解析开始的时候调用 */
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    
    _mulArray = [NSMutableArray array];
    NSLog(@"文档解析开始");
}

/** 2. 碰到开始标签时候调用 */
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    
    if ([elementName isEqualToString:@"Note"]) {
        _key = attributeDict.allValues[0];
        _mulDict = [[NSMutableDictionary alloc] init];
        _startTag = elementName;
        NSLog(@"开始标签 : %@ 属性 : %@",elementName,attributeDict);
    }
}

/** 3. 碰到两个标签之间的内容的时候调用 */
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // 去掉空格和换行  trim整齐
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([string isEqualToString:@""]) {
        return ;
    }
    [_mulDict setObject:string forKey:_startTag];
    NSLog(@"两个标签之间的内容 :  %@",string);
}

/** 4. 碰到结束标签时候调用 */
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    if ([elementName isEqualToString:@"Note"]) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:_mulDict forKey:_key];
        [_mulArray addObject:dict];
    }
    [NSString stringWithFormat:@"/%@",elementName];
    NSLog(@"结束标签 :  %@",[NSString stringWithFormat:@"/%@",elementName]);
    printf("\n");
}

/** 5. 文档解析结束的时候调用 */
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    
    NSLog(@"---------%@",_mulArray);
    NSLog(@"文档解析结束");
}

@end

<?xml version="1.0" encoding="UTF-8"?>
<Notes>
  <Note id="1">
    <CDate>2012-12-21</CDate>
    <Content>早上8点钟到公司</Content>
    <UserID>Allen</UserID>
  </Note>
  <Note id="2">
    <CDate>2012-12-22</CDate>
    <Content>开发企业微信 模块一</Content>
    <UserID>Allen</UserID>
  </Note>
  <Note id="3">
    <CDate>2012-12-23</CDate>
    <Content>开发企业微信 模块二</Content>
    <UserID>Allen</UserID>
  </Note>
  <Note id="4">
    <CDate>2012-12-24</CDate>
    <Content>开发企业微信 模块三</Content>
    <UserID>Allen</UserID>
  </Note>
  <Note id="5">
    <CDate>2012-12-25</CDate>
    <Content>开发企业微信 模块四</Content>
    <UserID>Allen</UserID>
  </Note>
  <Note id="6">
    <CDate>2012-12-26</CDate>
    <Content>开发企业微信 上线</Content>
    <UserID>Allen</UserID>
  </Note>
</Notes>
原文链接:https://www.f2er.com/xml/296054.html

猜你在找的XML相关文章