LessonUIParse

//saxParse

步骤:

1.从包中查找文件路径

NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];

2.根据文件路径生成同一资源定位符

NSURL *url = [[NSURL alloc] initFileURLWithPath:urlString];

3.根据生成的统一资源定位符(url), 创建解析类

NSXMLParser *saxParser = [[NSXMLParser alloc] initWithContentsOfURL:url ];

4.设置代理delegate, 因为所有的解析结果都在代理方法中(遵守代理)

saxParser.delegate = self;

5.开始解析

[saxParser parse];

常用的代理中的方法:(6个)

//已经开始解析

- (void)parserDidStartDocument:(NSXMLParser *)parser {

//开始时初始化

self.studentArray = [[NSMutableArray alloc] initWithCapacity:0];

}

//已经结束解析

- (void)parserDidEndDocument:(NSXMLParser *)parser {

NSLog(@"已经结束解析");

NSLog(@"%@", self.studentArray);

}

//解析出错

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {

}

//遇到开始标签, 比如<>

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

//elementName, 标签名字, 比如name

//namespaceURI, 命名空间

//qualifiedName, 命名空间的限定

//attributes, 标签的属性, 只能写在开始标签内

}

//遇到结束标签, 比如</>

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

}

//发现字符串

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

self.tempString = string;

}

####################

domParse

步骤:

1.从包中获取文件路径

NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];

2.根据文件路径生成url

NSURL *url = [[NSURL alloc] initFileURLWithPath:urlString];

3.根据url, 查找文件, 读取文件内容

NSString *contentString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

[url release];

4.根据文件内容创建Gdata的文档管理类, 及解析完成

GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:contentString options:0 error:nil];

5.提取数据

//先找到根节点, Students

GDataXMLElement *rootElement = [document rootElement];

NSLog(@"%@", rootElement);

//找子节点, Student

NSArray *studentElements = [rootElement elementsForName:@"Student"];

NSLog(@"%@", studentElements);

//封装数据

self.studentArray = [NSMutableArray arrayWithCapacity:0];

for (GDataXMLElement *element in studentElements) {

Student *student = [[Student alloc] init];

student.name = [[[element elementsForName:@"name"] firstObject] stringValue];

student.age = [[[element elementsForName:@"age"] firstObject] stringValue];

student.gender = [[[element elementsForName:@"gender"] firstObject] stringValue];

[self.studentArray addObject:student];

[student release];

}

NSLog(@"%@", self.studentArray);

}

##########################################

JSON解析

步骤:

//1.获取路径

NSString *pathString = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];

//2.根据文件路径, 创建data

NSData *data = [[NSData alloc] initWithContentsOfFile:pathString];

NSLog(@"%@", data);

//3.解析

//参数1:待解析的数据

//参数2:解析的参数配置

//a.NSJSONReadingMutableContainers, 字典或数组是否可变

//b.NSJSONReadingMutableLeaves, 字符串是否可变

//c.NSJSONReadingAllowFragments, 是否允许数据格式不完整

//参数3:解析是否发生错误

NSError *error = nil;

NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

if (error) {

NSLog(@"解析错误%@", error);

} else {

NSLog(@"%@", array);

NSMutableArray *studentArray = [[NSMutableArray alloc] initWithCapacity:0];

for (NSDictionary *dic in array) {

Student *student = [[Student alloc] init];

student.name = [dic valueForKey:@"name"];

student.age = [dic valueForKey:@"age"];

student.gender = [dic valueForKey:@"gender"];

[studentArray addObject:student];

[student release];

}

NSLog(@"%@", studentArray);

}

}

时间: 2024-10-12 19:06:54

LessonUIParse的相关文章