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 9257047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:06:43+00:00 2026-06-18T12:06:43+00:00

I had parsed the XML file from remote web server successfully. My XML contains

  • 0

I had parsed the XML file from remote web server successfully. My XML contains several elements with image links in it. i want to assign the images on different UIImageView’s for that i tried in the below way but got failed as am getting all the images of the elements. Due to this i can’t assign a particular image for a particular UIImageView. Can anyone suggest me how to do this,

MyXML

<?xml version="1.0" encoding="UTF-8"?>
<Products>
  <Main id="0">
   <name>Main</name>
   <mainimage id="1">http://sample.com/images/first.png</mainimage>
   <mainimage id="2">http://sample.com/images/second.png</mainimage>
  </Main>
  <Category id="1">
   <name>category1</name>
   <categoryimage id="1">http://sample.com/images/img1.png</categoryimage>
  </Category>
  <Category id="2">
   <name>category2</name>
   <categoryimage id="2">http://sample.com/images/img2.png</categoryimage>
   <subcategoryimage id="1">http://sample.com/images/img5.png</subcategoryimage>
   <subcategoryimage id="2">http://sample.com/images/img4.png</subcategoryimage>
  </Category>
</Products>

XML Parsing

- (BOOL)loadXMLByURL:(NSURL *)url
  {
     self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
     self.parser.delegate = self;
     return [self.parser parse];
  }

- (void)parserDidStartDocument:(NSXMLParser *)parser
  {
    self.products = [[NSMutableArray alloc] init];
  }
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
  if([elementName isEqualToString:@"mainimage"]){
     self.currentProduct = [[EGSProduct alloc] init];
     self.currentProduct.imageIdentifier = attributeDict[@"id"];
  }
if ([elementName isEqualToString:@"Category"]){
     self.currentProduct = [[EGSProduct alloc] init];
     self.currentProduct.imageIdentifier = attributeDict[@"id"];
  }
else if ([elementName isEqualToString:@"subcategoryimage"]){
     self.currentSubProduct = [[EGSProduct alloc] init];
     self.currentSubProduct.imageIdentifier = attributeDict[@"id"];
     if (self.currentProduct.subProducts == nil){
         self.currentProduct.subProducts = [NSMutableArray array];
     }
     self.currentSubProduct.imageIdentifier = attributeDict[@"id"];
     self.currentElementContent = [[NSMutableString alloc] init];
  }
else if ([elementName isEqualToString:@"name"] || [elementName isEqualToString:@"categoryimage"]){
     self.currentElementContent = [[NSMutableString alloc] init];
  }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
   if ([elementName isEqualToString:@"mainimage"]){
       self.currentProduct.name = self.currentElementContent;
       self.currentElementContent = nil;
   }
if ([elementName isEqualToString:@"name"]){
    self.currentProduct.name = self.currentElementContent;
    self.currentElementContent = nil;
   }
else if ([elementName isEqualToString:@"categoryimage"]){
self.currentProduct.imageUrlString = [self.currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
self.currentElementContent = nil;
}
else if ([elementName isEqualToString:@"subcategoryimage"]){
    self.currentSubProduct.imageUrlString = self.currentElementContent;
    self.currentElementContent = nil;
    [self.currentProduct.subProducts addObject:self.currentSubProduct];
    self.currentSubProduct = nil;
}
else if ([elementName isEqualToString:@"Category"]){
    [self.products addObject:self.currentProduct];
    self.currentProduct = nil;
 }
}

In View Controller when i tired to get the images of Main element am getting only the Category Element of the XML.

View Controller

[super viewDidLoad];
 NSURL *url = [NSURL URLWithString:@"http://www.sample.com/Category.xml"];
 EGSParser *parser = [[EGSParser alloc] init];
 if ([parser loadXMLByURL:url])
    self.xmlProductsResults = parser.products;
    EGSProduct *prod = (EGSProduct *)[self.xmlProductsResults objectAtIndex:0];
    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:prod.imageUrlString]];
    self.firstImage.image = [UIImage imageWithData:mydata];
 }
  • 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-18T12:06:44+00:00Added an answer on June 18, 2026 at 12:06 pm

    I think you are not able to get specific image for specific imageview. I think you are trying to get MainImage, CategoryImage and SubCategoryImage separately.. And trying to use them accordingly. You can reformat your data in dictionary like below

    { “mainimage”:[“image1″,”image2”], “categoryimage”:[“image1″,”image2”], “subcategoryimage”:[“image1″,”image2”] }

    You can achieve the above by using 3 MutableArray for your 3 categories. And just store data as required like below

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
      if([elementName isEqualToString:@"Products"]){
         mainImages=[[NSMutableArray alloc] init];
         catImages=[[NSMutableArray alloc] init];
         subCatImages=[[NSMutableArray alloc] init];
      }
    
    }
    

    Then in didend

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    
       if ([elementName isEqualToString:@"mainimage"]){
           [mainImages addObject:self.currentElementContent];
    
       }else if ([elementName isEqualToString:@"categoryimage"]){
           [catImages addObject:self.currentElementContent];
    
       }else if ([elementName isEqualToString:@"subcategoryimage"]){
           [subCatImages addObject:self.currentElementContent];
    
       }
    
       self.currentElementContent = nil;
     }
    

    I think this is want you want.. If want to use more information you can use either dictionary or, class objects.

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

Sidebar

Related Questions

When i retrieve the XML file from URL, i can Parse all the elements
I want to parse an XML file from URL using JDOM. But when trying
I need to insert data from my parsed XML file to mySQL table. Problem
I'm using lxml as follows to parse an exported XML file from another system:
I have a Service that downloads a file from the internet. What I want
Using the NSXMLParser i can retrieve the data from the local XML file, but
I'm trying to parse an xml file output from googletest to display the results
So I have an XML file here, and I want to get a value
I have an XML file that I want to parse into a database in
Let's say I had an xml file like this: <items> <item> <id>1</id> <name>Item 1</name>

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.