XML、JSON数据解析
{
//xml解析:逐行解析
//从文件列表中读取数据
//1.获得文件路径
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@".xml"];
//2.通过路径把文件转换成NSData类型
NSData *data = [NSData dataWithContentsOfFile:sourcePath];
//创建的时候需要给parser一个字符串数据(NSData)
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
//设定代理人
parser.delegate = self;
//开始对文件进行解析
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
//当找到节点的时候,系统调用这个方法
NSLog(@"节点头");
if ([elementName isEqualToString:@"students"]) {
//当找到students节点的时候,初始化数组
self.arr = [NSMutableArray array];
}else if ([elementName isEqualToString:@"student"]) {
//当找到student节点的时候,创建一个student对象 添加到数组里
Student *stu = [[Student alloc] init];
[self.arr addObject:stu];
[stu release];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//当节点找到内容的时候,调用
NSLog(@"节点内容");
//把内容保存起来
self.tempStr = string;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//当找到节点结尾的时候调用
NSLog(@"节点尾");
Student *stu = [self.arr lastObject];
if ([elementName isEqualToString:@"number"]) {
stu.number = self.tempStr;
}else if ([elementName isEqualToString:@"name"]){
stu.name = self.tempStr;
}else if ([elementName isEqualToString:@"sex"]){
stu.sex = self.tempStr;
}else if ([elementName isEqualToString:@"phone"]){
stu.phone = self.tempStr;
}
}
{
//按节点解析
//1.获取解析的文件信息
NSString *xmlPath = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@".xml"];
NSData *data = [NSData dataWithContentsOfFile:xmlPath];
//参数1:要解析的xml串 参数2:随便 参数3:错误信息
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
//2.获取文件的根节点
GDataXMLElement *rootElement = [doc rootElement];
//3.进一步搜索所有的子节点
//返回一个装满了student节点(GDataXMLElement对象)的数组
NSArray *stuArrray = [rootElement elementsForName:@"student"];
//4.遍历数组,把student节点的每个子节点取出来
for (GDataXMLElement *element in stuArrray) {
Student *stu = [[Student alloc] init];
GDataXMLElement *nameElement = [[element elementsForName:@"name"] lastObject];
//从name节点中取值
NSString *value = [nameElement stringValue];
//给学生对象的属性赋值
stu.name = value;
GDataXMLElement *numberElement = [[element elementsForName:@"number"] lastObject];
value = [numberElement stringValue];
stu.number = value;
[self.stuDomArray addObject:stu];
[stu release];
}
}
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error = nil;
//参数1:文件数据 参数2:设置解析的结果 参数3:错误信息
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",array);
for (NSDictionary *dic in array) {
Student *stu = [[Student alloc] init];
stu.name = [dic objectForKey:@"name"];
stu.sex = [dic objectForKey:@"sex"];
stu.phone = [dic objectForKey:@"phone"];
stu.number = [dic objectForKey:@"number"];
[self.stuArray addObject:stu];
[stu release];
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。