使用NSXMLParser进行XML解析 - 我需要进行代码审查

时间:2021-10-12 19:38:09

I just need confirmation that I am doing XML parsing right. Am I doing something wrong here:

我只需要确认我正在进行XML解析。我在这里做错了什么:

@interface RootViewController : UITableViewController <NSXMLParserDelegate> 
{   
    NSXMLParser *parser;    
    NSMutableArray *ordersList; 

    BOOL orderIdFound;
    BOOL itemNameFound;
    BOOL priceFound;
    BOOL orderDateFound;
    BOOL deliveryDateFound;

    Order *orderObject;
}

@property (nonatomic, retain) NSMutableArray *ordersList;
@property (nonatomic, retain) NSXMLParser *parser;
@property (nonatomic, retain) Order *orderObject;

@end


@implementation RootViewController

@synthesize ordersList;
@synthesize parser;
@synthesize orderObject;

- (void)viewDidLoad 
{
    [super viewDidLoad];

    ordersList = [[NSMutableArray alloc]init];

    // Reading local XML file
    NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"orders" ofType:@"xml"];

    NSData *data = [[NSData alloc]initWithContentsOfFile:xmlFilePath];
    parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = self;
    [parser setShouldResolveExternalEntities:YES];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
{       
    if ([elementName isEqualToString:@"OrderId"])
    {           
        orderIdFound = YES;
        if(orderObject == nil)
        {
            orderObject = [[Order alloc]init];
        }
        else 
        {
            [ordersList addObject:orderObject];

            [orderObject release];
            orderObject = nil;
            orderObject = [[Order alloc]init];
        }
    }
    if ([elementName isEqualToString:@"ItemName"])
    {
        itemNameFound = YES;
    }
    if ([elementName isEqualToString:@"Price"])
    {
        priceFound = YES;
    }
    if ([elementName isEqualToString:@"OrderDate"])
    {
        orderDateFound = YES;   
    }
    if ([elementName isEqualToString:@"DeliveryDate"])
    {
        deliveryDateFound = YES;
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{    
    if([elementName isEqualToString:@"OrderId"])
    {
        orderIdFound = NO;      
    }
    if([elementName isEqualToString:@"ItemName"])
    {
        itemNameFound = NO;     
    }
    if([elementName isEqualToString:@"Price"])
    {
        priceFound = NO;        
    }
    if([elementName isEqualToString:@"OrderDate"])
    {
        orderDateFound = NO;        
    }
    if([elementName isEqualToString:@"DeliveryDate"])
    {
        deliveryDateFound = NO;
    }
}

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

    if(orderIdFound == YES)
    {
        orderObject.OrderId = [string integerValue];
        NSLog(@"orderId :: %i", orderObject.OrderId);

    }
    if(itemNameFound == YES)
    {       
        orderObject.ItemName = string;
        NSLog(@"itemName :: %@", orderObject.ItemName);
    }
    if(priceFound == YES)
    {       
        orderObject.Price = [string integerValue];
        NSLog(@"price :: %i", orderObject.Price);
    }
    if(orderDateFound == YES)
    {       
        orderObject.OrderDate = string;
        NSLog(@"orderDate :: %@", orderObject.OrderDate);
    }
    if(deliveryDateFound == YES)
    {       
        orderObject.DeliveryDate = string;
        NSLog(@"deliveryDate :: %@", orderObject.DeliveryDate);
    }
}

1 个解决方案

#1


2  

It looks generally ok, except that you're leaking memory. Any calls to alloc, new, copy or retain need to balanced with a release (e.g. in dealloc).

它看起来一般都没问题,除了你在泄漏记忆。对alloc,new,copy或retain的任何调用都需要与release(例如dealloc)进行平衡。

Also, I would suggest not doing XML parsing in a view controller. XML parsing is a seperate concern and is suitable for factoring out into a seperate class.

另外,我建议不要在视图控制器中进行XML解析。 XML解析是一个单独的问题,适合分解为单独的类。

#1


2  

It looks generally ok, except that you're leaking memory. Any calls to alloc, new, copy or retain need to balanced with a release (e.g. in dealloc).

它看起来一般都没问题,除了你在泄漏记忆。对alloc,new,copy或retain的任何调用都需要与release(例如dealloc)进行平衡。

Also, I would suggest not doing XML parsing in a view controller. XML parsing is a seperate concern and is suitable for factoring out into a seperate class.

另外,我建议不要在视图控制器中进行XML解析。 XML解析是一个单独的问题,适合分解为单独的类。