When i retrieve the XML file from URL, i can Parse all the elements from the XML file and view in log file, my parsing code looks like this,
-(id) loadXMLByURL:(NSString *)urlString{
tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Products"]){
currentTweet = [egsBase alloc];}}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"img"]){
currentTweet.img=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:currentNodeContent]]];}
if ([elementName isEqualToString:@"Products"]) {
[tweets addObject:currentTweet.img];
currentTweet = nil;
currentNodeContent = nil;}}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];}
I use the class name for parsing as XmlParsing . In the controller class i had implement the class file XmlParsing as XmlParsing xmlparser; Now in viewDidLoad() function i use code like this,
- (void)viewDidLoad{
[super viewDidLoad];
xmlParser = [[XMLParsing alloc] loadXMLByURL:@"http://images.com/Products.xml"];
UIImage *currentImage = [[xmlParser tweets] objectAtIndex:0];
customimage.image = currentImage;}
Here the customimage is reference for UIImageView, Now the problem is i can get only the last element value and show in the UIImageView connected in story board (ie) for better understanding it shows only the last element image and not the previous elements i use in elements. how to solve this ? where am doing wrong. Kindly suggest me.
STRUCTURE OF XML
<Products>
<products id="1">
<img>http://images.com/images/Main_pic.png</img>
</products>
<products id="2">
<img>http://images.com/images/image1.png</img>
</products>
</Products>
My NSLog shows for parsing XML file,
images <UIImage: 0x71529a0>
images <UIImage: 0x74a6750>
When i print to check what image i get it shows the current image as,
Current,<UIImage: 0x74a6750>
here you can see the current image shows the last element alone not the previous one
I have added line numbers for ease of understanding :
Line number 3 : You always get a new image (Am i right?)
Line number 4 : whenever u get a new image you set that image to the same
imageview(customimage) due to this previous images gets replaced
with the new one.
Ideally you should set every new image to respective image views.
If u r trying to load these images in a table view —
Then name of image view remains same but actually the object is different so you should access the imageview of each cell by its TAG assigned in cellForRowAtIndexPath.
Updated Code
Desc : while parsing when “products” is found as start element and object gets created, and when end element is found image gets associated to objects and gets added to array.
I see the error is : you are using “Products” in parsing condition instead of “products”
Hope the idea helps
🙂