我还必须以 XML 格式解析标签和子标签的数量,那么在解析和获取要显示的数据时如何使其平滑?
请提出正确的解析方法。
//使用 NSXMLParser
NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"YOUR_URL"]];
[parser setDelegate:self];
[parser parse];
// 下面是将为您获取数据的委托
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"forecast_conditions"]){
getData = YES; // getData is a bool which is NO initialy
}
if(getData)
{
if([elementName isEqualToString:@"day_of_week"])
NSLog(@"%@",[attributeDict objectForKey:@"data"]);
if([elementName isEqualToString:@"low"])
NSLog(@"%@",[attributeDict objectForKey:@"data"]);
if([elementName isEqualToString:@"high"])
NSLog(@"%@",[attributeDict objectForKey:@"data"]);
if([elementName isEqualToString:@"icon"])
NSLog(@"%@",[attributeDict objectForKey:@"data"]);
if([elementName isEqualToString:@"condition"])
NSLog(@"%@",[attributeDict objectForKey:@"data"]);
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"forecast_conditions"]){
getData = NO;
}
}