iOS XML  解析(原生的)

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#import "AppDelegate.h"
#import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "NoteXMLParser.h"
@interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
NoteXMLParser *parser = [[NoteXMLParser alloc] init];
[parser start];
} @end
#import <Foundation/Foundation.h>

@interface NoteXMLParser : NSObject<NSXMLParserDelegate>

@property (strong, nonatomic) NSMutableArray *notes;

/**
* 当前标签的名字,currentTagName 用于存储正在解析的元素名
*/
@property (strong, nonatomic) NSString *currentTagName; /**
* 开始解析
*/
- (void)start; @end
#import "NoteXMLParser.h"

@implementation NoteXMLParser

// 开始解析
- (void)start{
NSString *path = [[NSBundle mainBundle] pathForResource:@"some" ofType:@"xml"];
NSURL *url = [NSURL fileURLWithPath:path]; // 开始解析xml
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
parser.delegate = self; [parser parse]; }
/**
* 解析到文档的开头时会调用
*/
- (void)parserDidStartDocument:(NSXMLParser *)parser{
_notes = [[NSMutableArray alloc] init];
} /**
* 解析到一个元素的开始就会调用
*
* @param elementName 元素名字
* @param attributeDict 属性字典
*/
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
/* 一般情况,如果数据是这种格式,把内容放到属性中
<CrReportByHour Hour=@"10" Caption=@"10-12" CrChannel=@"0.8" CrRegion=@"0.6" CrArea=@"0.7"/>
系统会自动把以上内容转为字典存放到attributeDict这个字典中
可以用KVC通过字典给模型直接复制(字典中的key必须都能在模型中找到)
*/
_currentTagName = elementName;
NSLog(@"====elementName: %@~~~namespaceURI:%@~~~attributeDict=%@---qName=%@",elementName,namespaceURI,attributeDict,qName);
}
// 遇到字符串时触发
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//替换回车符 和空格,其中 stringByTrimmingCharactersInSet 是剔除字符的方法,[NSCharacterSet whitespaceAndNewlineCharacterSet]指定字符集为换行符和回车符;
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([_currentTagName isEqualToString:@"Hour"]) {
NSLog(@"*************string:%@",string);
} }
/**
* 解析到一个元素的结束就会调用
* @param elementName 元素名字
*/
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"结束的元素名称:%@",elementName);
}
/**
* 解析到文档的结尾时会调用(解析结束)
*/
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"结束解析");
} //文档出错时触发
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"解析出错");
} @end
上一篇:CentOS 7 之 Systemd 入门教程:命令篇


下一篇:python中对变量的作用域LEGB、闭包、装饰器基本理解