加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML解析1_XMLParser

发布时间:2020-12-16 08:53:52 所属栏目:百科 来源:网络整理
导读:#import "ViewController.h"#import "TBXML.h"@interface ViewController () NSXMLParserDelegate@property (nonatomic,strong) NSMutableArray *mulArray;@property (nonatomic,strong) NSMutableDictionary *mulDict;@property (nonatomic,copy) NSString
#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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读