Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:17:55+00:00 2026-06-15T08:17:55+00:00

When i retrieve the XML file from URL, i can Parse all the elements

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:18:03+00:00Added an answer on June 15, 2026 at 8:18 am
    - (void)viewDidLoad
    {
     1) [super viewDidLoad];
     2) xmlParser = [[XMLParsing alloc] loadXMLByURL:@"http://images.com/Products.xml"];
     3) UIImage currentImage = [[xmlParser tweets] objectAtIndex:0];
     4) customimage.image = currentImage;
    }
    

    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

    -(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]]];
        [tweets addObject:currentTweet.img];
       }
     if ([elementName isEqualToString:@"products"])
      {
       currentTweet = nil;
       currentNodeContent = nil;
      }
    }
    

    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
    🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I retrieve the XML source file name using XSL 1.0 code?
I have a code to retrieve an XML file from the site, so I
basically I am parsing a xml file and retrieving certain elements from that xml
My task is to retrieve data from XML file. Every node in XML file
this is my xml file,i do not known how to retrieve value from xml
I am trying to retrieve content from xml tagnames from a url, but my
What's the simplest way in PHP, to: Retrieve a file from an external URL
I tried to retrieve XML Google weather API from this URL: http://www.google.com/ig/api?weather=,,,24467280,39606640&hl=ar It is
I have some code to retrieve XML data: import cStringIO import pycurl from xml.etree
I am using XmlReader.Create to retrieve data from an RSS xml file. Then I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.