Mahesh Babu
Mahesh Babu

Reputation: 3433

need help to parse xml string

i need to parse this xml file

<?xml version="1.0" encoding="UTF-8"?>
<imageset>
<category>
<image>SQUIRREL</image>
<image>FOX</image>  
<image>TIGER</image>    
<image>LION</image> 
</category>
</imageset>

i use this code to parse this.

- (void)viewDidLoad {
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Config.xml"]]];
    [super viewDidLoad];
    [parser setDelegate:self];
    [parser parse];
    NSLog(@"test.......... %@",test);
    }
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    if ([elementName isEqualToString:@"image"]) {
        test = [[NSMutableArray alloc]init];
        addelements = TRUE;
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
    if ([elementName isEqualToString:@"image"]) {
        addelements = FALSE;
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        [test addObject:string];
}

But it adds only last object,displays like this in console.

test.......... (
    LION,
    "\n\n",
    "\n"
)

I need to add all image names in the xml file means {SQUIRREL,FOX,TIGER,LION}

how can i done,can any one please help me.

Thank u in advance.

Upvotes: 1

Views: 544

Answers (3)

Sabby
Sabby

Reputation: 2592

@Mahesh Babu yes you can also add the image  by giving the name only as for example......    

[UIImage imageNamed:@"SQUIRREL.jpg"];

Upvotes: 1

DarkDust
DarkDust

Reputation: 92316

Your problem is this method:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    if ([elementName isEqualToString:@"image"]) {
        test = [[NSMutableArray alloc]init];
        addelements = TRUE;
    }
}

Every time an image tag starts, you overwrite the test variable with a new instance of NSMutableArray (leaking the previous object). You need to change it to something like this:

        if (!test) test = [[NSMutableArray alloc]init];

That is: only allocate a new array if there wasn't an old one already.

Upvotes: 3

Max
Max

Reputation: 16719

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    [mutableString release];
    mutableString  = [[NSMutableString alloc] init];           
    if ([elementName isEqualToString:@"image"]) {
        if( !test  )  test = [[NSMutableArray alloc] initWithCapacity: 10];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{   
    if ([elementName isEqualToString:@"image"])  
        [test addObject: mutableString];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
       [mutableString appendString: string];
}

where test and mutableString are ivars declared in your view controller's header

Note: this is very simplified version.

Upvotes: 2

Related Questions