views:

175

answers:

2

I have 3 classes->

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

And then in my app delegate I defined-> NSMutableArray categories;

I parse an XML file with this code. I try the arrays hierachy, and i think that i don't add any object on the arrays. How can I check it? What's wrong?

- (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; 

And here is a short sample of XML file:

<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&lt;/urlVideo&gt;
</video>
</Topic>
</Category>
</Videos>
A: 

It doesn't look like aTopic.videos is ever alloc'd/init'd.

Right after you create aTopic in didStartElement, do alloc+init on aTopic.videos.

DyingCactus
I add this aTopic.videos = [[NSMutableArray alloc] init]; on didStartElement but the problem is the same. The count returns 0. HEre is where I add: else if ([elementName isEqualToString:@"Topic"]) { aTopic = [[Topic alloc] init]; aTopic.videos = [[NSMutableArray alloc] init]; aTopic.topicID = [[attributeDict objectForKey:@"id"] integerValue]; NSLog(@"Reading id topic value: %i", aTopic.topicID); }
skiria
Where you log the count, also log just aTopic (eg NSLog("aTopic = %@", aTopic);) to make sure it is not also nil. Maybe it is getting released before the video element ends.
DyingCactus
I get <Topic: 0x383d710> <Topic: 0x383c540>... a different value for every topic element. What it means? Where might be the error?
skiria
So the topics are not nil. They are different because you create a new aTopic for each Topic element. Not sure what the problem is. Post a short but complete sample of the xml (add it to the question). Stepping through the code in the debugger might also help you.
DyingCactus
Found one more issue: aCategory.topics is also not getting created when creating aCategory. Add this line after the alloc+init for aCategory in didStartElement: `aCategory.topics = [[NSMutableArray alloc] init];`
DyingCactus
I also add when I've told me that. The counts is also 0.
skiria
I don't see anything else wrong in the posted code. The actual code you are running might be different or there may be additional problems in the rest of the code not shown in your post. Is the actual XML different from the sample? Post the full code or the smallest complete app that duplicates the problem.
DyingCactus
The XML is the same, I've only cut it to paste here a short sample. Maybe there are some additional problems.I detected the problem when i try to use the arrays to build a tableview and it doesn't show anything on the second view, so I check the array and the count is 0. How can I post all the project? Maybe I can send you...
skiria
Being able to see the array on another view is a separate problem than the above. Immediately after calling parse, check the categories array and arrays inside each object. Are they filled? First get that working then worry about the other view.
DyingCactus
categories.topicID and catgories.nameCategory are ok. Catgories.topics is 0, but then ATopic.nameTopic, aVideo.nameVideo, aVideo.user... all of this are ok. Every independ object: aVideo, aTopic and aCategory are ok, but all the arrays inside, have 0 items (count array).The code that I post here is all that i use to parse the XML file.
skiria
Show the .h file for all the 3 classes and the .h file for the parser class and how the categories array is defined in the AppDelegate. I want to see exactly how the classes, ivars, properties, etc are defined.
DyingCactus
Thanks for posting all that info. I tested all the code as posted and it works fine AFTER adding the two alloc+init lines. I also tried showing the categories on a table view in another view and that also worked fine. Any remaining problem must be elsewhere.
DyingCactus
I don't know what's wrong. I check my code of tableview, maybe the problem is there.
skiria
Solved! I don't know the error but I post the xml file in another server and it works good. Thanks for the patience, the answers and thanks for all. I'm very grateful!
skiria
A: 

can u please send the code for how to read the videos from xml

thanks in advance

Dhanunjaya