2010-05-07 88 views
0

我有3类 - >如何检查数组的内容?解析XML文件的ObjectiveC

video { nameVideo, id, description, user... } 
topic {nameTopic, topicID, NSMutableArray videos; } 
category {nameCategory, categoryID, NSMUtableArray topics} 

然后在我的应用程序委托我defined->的NSMutableArray类;

我用这段代码解析XML文件。我尝试数组hierachy,并且我认为我不会在数组中添加任何对象。我如何检查它?怎么了?

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

    if([elementName isEqualToString:@"Videos"]) { 
     //Initialize the array. 
     appDelegate.categories = [[NSMutableArray alloc] init]; 
    } 


    else if ([elementName isEqualToString:@"Category"]) 
    { 
     aCategory = [[Category alloc] init]; 

     aCategory.categoryID = [[attributeDict objectForKey:@"id"] integerValue]; 

     NSLog(@"Reading id category value: %i", aCategory.categoryID); 

    } 

    else if ([elementName isEqualToString:@"Topic"]) { 
     aTopic = [[Topic alloc] init]; 

     aTopic.topicID = [[attributeDict objectForKey:@"id"] integerValue]; 

     NSLog(@"Reading id topic value: %i", aTopic.topicID); 

    } 

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

     aVideo = [[Video alloc] init]; 

     aVideo.videoID = [[attributeDict objectForKey:@"id"] integerValue]; 
     aVideo.nameTopic = currentNameTopic; 
     aVideo.nameCategory = currentNameCategory; 

     NSLog(@"Reading id video value: %i", aVideo.videoID); 

    } 


    NSLog(@"Processing Element: %@", elementName); 
} 



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

    if(!currentElementValue) 
     currentElementValue = [[NSMutableString alloc] initWithString:string]; 
    else 
     [currentElementValue appendString:string]; 

    NSLog(@"Processing Value: %@", currentElementValue); 

} 

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

    if([elementName isEqualToString:@"Videos"]) 
     return; 

    if ([elementName isEqualToString:@"Category"]) { 
     [appDelegate.categories addObject:aCategory]; 
     [aCategory release]; 
     aCategory = nil; 
    } 


    if ([elementName isEqualToString:@"Topic"]) { 
     [aCategory.topics addObject:aTopic]; 
     //NSLog(@"contador: %i", [aCategory.topics count]); 
     //NSLog(@"contador: %@", aTopic.nameTopic); 

     [aTopic release]; 
     aTopic = nil; 
    }   

    if ([elementName isEqualToString:@"video"]) { 
     [aTopic.videos addObject:aVideo]; 
     NSLog(@"count number videos: %i", [aTopic.videos count]); --> always 0 
     NSLog(@"NOM CATEGORIA VIDEO: %@", aVideo.urlVideo); --> OK 

     [aVideo release]; 
     aVideo = nil; 
    } 

    if ([elementName isEqualToString:@"nameCategory"]) { 
     //[aCategory setValue:currentElementValue forKey:elementName]; 
     aCategory.nameCategory = currentElementValue; 
     currentNameCategory = currentElementValue; 
    } 

    if ([elementName isEqualToString:@"nameTopic"]) { 
     aTopic.nameTopic = currentElementValue; 
     currentNameTopic = currentElementValue; 
    } 


    else 
    [aVideo setValue:currentElementValue forKey:elementName]; 

    [currentElementValue release]; 
    currentElementValue = nil; 

} 

//xmlparser.h

@class TSCTerrassaAppDelegate, Category, Topic, Video; 
@interface XMLParser : NSObject { 

    NSMutableString *currentElementValue; 
    NSMutableString *currentNameCategory; 
    NSMutableString *currentNameTopic; 

    TSCTerrassaAppDelegate *appDelegate; 

    Video *aVideo; 
    Topic *aTopic; 
    Category *aCategory; 
} 

@property (nonatomic, retain)NSMutableString *currentNameCategory; 
@property (nonatomic, retain) NSMutableString *currentNameTopic; 

//Video.h

@interface Video : NSObject { 

    NSInteger videoID; 
    NSString *nameVideo; 
    NSString *nameCategory; 
    NSString *nameTopic; 
    NSString *user; 
    NSString *date; 
    NSString *description; 
    NSString *urlThumbnail; 
    NSString *urlVideo; 
} 

//Topic.h

@class Video; 

@interface Topic : NSObject { 
    NSMutableString *nameTopic; 
    NSInteger topicID; 
    NSMutableArray *videos; 
} 

@property (nonatomic, readwrite) NSInteger topicID; 
@property (nonatomic, retain) NSMutableArray *videos; 
@property (nonatomic, retain) NSMutableString *nameTopic; 

//Category.h

@class Topic, Video; 

@interface Category : NSObject { 
    NSMutableString *nameCategory; 
    NSInteger categoryID; 
    NSMutableArray *topics; 
} 

@property (nonatomic, retain) NSMutableArray *topics; 
@property (nonatomic, readwrite) NSInteger categoryID; 
@property (nonatomic, retain) NSMutableString *nameCategory; 

// AppDelegate中

@class NavegaNavController; 

@interface TSCTerrassaAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    IBOutlet UITabBarController *rootController; 
    IBOutlet NavegaNavController *navegaNavController; 

    NSMutableArray *categories; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UITabBarController *rootController; 
@property (nonatomic, retain) IBOutlet NavegaNavController *navegaNavController; 

@property (nonatomic, retain) NSMutableArray *categories; 

这里是XML文件的一个简短的样本:

<Videos> 
<Category id="12"> 
<nameCategory>xxxx</nameCategory> 
<Topic id="43"> 
<nameTopic>zzzz</nameTopic> 
<video id="54"> 
<nameVideo>videoest1</nameVideo> 
<user>skiria</user> 
<date>24-11-09</date> 
<description>testing videos</description> 
<urlThumbnail>URLTHUMBNAIL</urlThumbnail> <urlVideo>http://bipbop/gear1/prog_index.m3u</urlVideo> 
</video> 
</Topic> 
</Category> 
</Videos> 

回答

0

它看起来并不像aTopic.videos是有史以来alloc'd/init'd 。

在didStartElement中创建主题后,在aTopic.videos上执行alloc + init。

+0

我加了这个aTopic.videos = [[NSMutableArray alloc] init];在didStartElement上,但问题是一样的。计数返回0. 是我在其中添加的: else if([elementName isEqualToString:@“Topic”]){ \t \t aTopic = [[Topic alloc] init]; \t \t aTopic.videos = [[NSMutableArray alloc] init]; \t \t \t \t aTopic.topicID = [[attributeDict objectForKey:@ “ID”] integerValue]; (@“读取标题主题值:%i”,aTopic.topicID); \t} – nabrugir 2010-05-07 14:21:09

+0

你在哪里记录计数,还记录只是一个主题(例如NSLog(“aTopic =%@”,aTopic);)以确保它不是零。也许它在视频元素结束之前得到释放。 – DyingCactus 2010-05-07 14:27:53

+0

我得到<主题:0x383d710><主题:0x383c540> ...为每个主题元素提供了不同的值。这是什么意思?哪里可能是错误? – nabrugir 2010-05-07 21:33:49