#import "RegisterDevice.h"
@interface RegisterDevice ()<NSXMLParserDelegate>
//解析出的数据内部是字典类型
@property(strong, nonatomic) NSMutableArray *listData;
//当前标签的名字
@property(strong, nonatomic) NSString *currentTagName;
@end
@implementation RegisterDevice
+ (NSDictionary *)registerDeviceWithLocalInfo:(NSDictionary *)localInfoDic{
NSMutableDictionary *mutDic = [[NSMutableDictionary alloc] init];
NSString *strURL = @"";
//把字符串转换为URL字符串
[strURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
if (!error) {
// NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//解析
[[[self alloc] init] start:data];
}else{
NSLog(@"error: %@", error.localizedDescription);
}
}];
[task resume];
return [NSDictionary dictionaryWithDictionary:mutDic];
}
- (void)start:(NSData *)data{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
}
//文档开始的时候触发
- (void)parserDidStartDocument:(NSXMLParser *)parser {
self.listData = [[NSMutableArray alloc] init];
}
//文档出错的时候触发
- (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 {
self.currentTagName = elementName;
if ([self.currentTagName isEqualToString:@"Table1"]) {
[self.listData addObject:attributeDict];
}
}
//遇到字符串时候触发
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//替换回车符和空格
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([string isEqualToString:@""]) {
return;
}
}
//遇到结束标签时候出发
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
self.currentTagName = nil;
}
//遇到文档结束时候触发
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"NSXML解析完成...");
// self.data.text = [NSString stringWithFormat:@"%@\nNSXML解析完成...", self.data.text];
//
// for (NSDictionary *dic in self.listData) {
// self.data.text = [NSString stringWithFormat:@"%@\n num: %@\t name: %@",self.data.text, [dic objectForKey:@"num"], [dic objectForKey:@"name"]];
// }
}
@end
ios & XML
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...