iOS-XML解析

2229 ワード

XML解析:
//  GData 
//  XML (       )
    //(1)         
    //      Header Search Paths-> /usr/include/libxml2
    //(2)      
    //  Link library ->  libxml2.dylib
    //(3)         
    //      -fno-objc-arc
    //(4)     
    //  #import "GDataXMLNode.h"


#import “GDataXMLNode.h"


//  xml      
- (void)xmlUse
{
    
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:_urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:responseObject options:0 error:nil];
        NSLog(@"%@",doc);
        NSArray *array = [doc nodesForXPath:@"//newslist" error:nil];
        for(GDataXMLElement *e in array)
        {
            
        NSString *id = [[[e elementsForName:@"id"] firstObject] stringValue];
        NSString *price = [[[e elementsForName:@"price"] firstObject] stringValue];
        NSString *name = [[[e elementsForName:@"name"] firstObject] firstObject];


        }


        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)xmlBasicUse
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"xml.txt" ofType:nil];
    NSData *data = [[NSData alloc]initWithContentsOfFile:path];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
    NSArray *array = [doc nodesForXPath:@"//promotion" error:nil];
    for(GDataXMLElement *e in array)
    {
        NSLog(@"name = %@",e.name);
        NSString *id = [[[e elementsForName:@"id"] firstObject] stringValue];
        NSString *price = [[[e elementsForName:@"price"] firstObject] stringValue];
        NSString *name = [[[e elementsForName:@"name"] firstObject] firstObject];
        
        NSLog(@"%@ - %@ - %@",id,name,price);
        
        PromotionModel *model = [[PromotionModel alloc]init];
        model.id = id;
        model.name = name;
        model.price = price;
    }
}