iOS开发-Day33-xml解析

1.系统自带(需要遵循协议NSXMLParserDelegate)

-(void)xml
{

    NSXMLParser *par=[[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.ieb2zbsc"]];
    par.delegate=self;
    [par parse];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"name"]) {
        [self.marr2 addObject:self.str];
    }else if([elementName isEqualToString:@"name"]){

    }else if([elementName isEqualToString:@"name"]){

    }
}
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.marr2=[NSMutableArray arrayWithCapacity:0];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{

}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    self.str=string;
}

2.第三方(需要导入GDataXMLNode文件,并修改工程配置)

-(void)Gxml
{
    NSString *path=@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.ieb2zbsc";

    NSURL *url=[NSURL URLWithString:path];

    NSURLRequest *request;

    NSURLResponse *response;
    __autoreleasing NSError *error;
    request=[NSURLRequest requestWithURL:url];
    NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    GDataXMLDocument *xml=[[GDataXMLDocument alloc]initWithData:data options:0 error:&error];
    GDataXMLElement *root=[xml rootElement];
//    NSLog(@"%@",root);
//    NSLog(@"%@",[root elementsForName:@"divisions"]);
    NSArray *arr=[root elementsForName:@"divisions"];
    for (GDataXMLElement *division in arr) {
        NSArray *arr2=[division elementsForName:@"division"];
        for (GDataXMLElement *inf in arr2) {
//             NSLog(@"%@",[[inf elementsForName:@"name"][0] stringValue]);
            [self.marr addObject:[[inf elementsForName:@"name"][0] stringValue]];
            NSArray *arr3=[inf elementsForName:@"location"];
            for (GDataXMLElement *local in arr3) {
                NSLog(@"%@",[[local elementsForName:@"latitude"][0] stringValue]);
                NSString *location=[NSString stringWithFormat:@"%@/%@",[[local elementsForName:@"latitude"][0] stringValue],[[local elementsForName:@"longitude"][0] stringValue]];
                [self.datilmarr addObject:location];
            }
        }
    }


}

你可能感兴趣的:(xml,ios)