1

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];
 }
Sabarish
  • 1,184
  • 3
  • 14
  • 35

4 Answers4

2

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.

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • Yes Exactly what i expected.. when i used the above code i got an error and app got terminated saying `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'` – Sabarish Feb 07 '13 at 10:25
  • self.currentElementContent = nil; should be in the end instead of first line in didEnd function.. change the code it should work.. – iphonic Feb 07 '13 at 10:42
  • am getting the app termination with same error when i debug i see the error comes with view controller code as i shown above. i changed the `self.xmlProductsResults = parser.products;` to `self.xmlProductsResults = parser.mainimages;` in view controller as above but still the app gets terminated. – Sabarish Feb 07 '13 at 10:52
1

you have to assign value to self.currentProduct.imageUrlString when you encounters the mainimage Element in didEndElement i.e.

if([elementName isEqualToString:@"mainimage"])
{   self.currentProduct.imageUrlString=self.currentElementContent;;

  }
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • I tried with your code and still am getting the `Category` elements only in the view controller. DO i need to change anything in view controller to get the `Main` elements – Sabarish Feb 07 '13 at 08:55
0

I'd Prefer simpler XML Parser for this case Have a look on this http://vuknikolic.wordpress.com/2012/04/27/super-easy-ios-xml-parsing/

TheGhost
  • 159
  • 1
  • 6
0

@Ravindra has given you correct answer, you need to manage your data in array or something. Also self.currentProduct.name = self.currentElementContent; This line replace your main image data. So better use dictionary structure for that

DivineDesert
  • 6,924
  • 1
  • 29
  • 61