1

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank u Dark Dust,I have a small doubt,there are the image names i need to add this image view what ever the extension it is, i mean i have images named SQUIRREL.jpg added in resources,how can i get it and display it in imageview
Add the ".jpg" extension via -[NSString stringByAppendingString:] and use that result as argument for -[UIImage imageNamed:]. Please read their respective class references for details.
is it possible to get by giving name i mean SQUIRREL,what ever the extension
2
- (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.

2 Comments

if i use this last element is added 3 times
maybe, i haven't tested this. I've just shown a concept of using the parser.
1
@Mahesh Babu yes you can also add the image  by giving the name only as for example......    

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

4 Comments

@Mahesh babu this is possible you can do this way as well ShadowImageView.image=[UIImage imageNamed:@"img"];
@Mahesh babu i tried this for you and it worked,what happened didn't get any reply or acceptance from your side...........Enjoy dude.....
No,it does n't display without extension
Sorry but worked for me.....I have images files but with extension PNG in bundle.....I just gave the file name without extension and it showed the image..Hard luck..Try with PNG images.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.