1

我已经成功地从远程 Web 服务器解析了 XML 文件。我的 XML 包含几个带有图像链接的元素。我想在不同的 UIImageView 上分配图像,因为我尝试了以下方式,但由于获取元素的所有图像而失败。因此,我无法为特定的 UIImageView 分配特定的图像。任何人都可以建议我如何做到这一点,

我的XML

<?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 解析

- (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;
 }
}

在视图控制器中,当我厌倦了获取Main元素的图像时,我只得到Category了 XML 的元素。

视图控制器

[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];
 }
4

4 回答 4

2

我认为您无法获得特定图像视图的特定图像。我认为您正在尝试分别获取 MainImage、CategoryImage 和 SubCategoryImage ..并尝试相应地使用它们。您可以在字典中重新格式化您的数据,如下所示

{ "mainimage":["image1","image2"], "categoryimage":["image1","image2"], "subcategoryimage":["image1","image2"] }

您可以通过为您的 3 个类别使用 3 MutableArray 来实现上述目标。并按要求存储数据,如下所示

- (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];
  }

}

然后在迪登

- (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;
 }

我认为这是您想要的。如果想使用更多信息,您可以使用字典或类对象。

于 2013-02-07T10:14:30.720 回答
1

self.currentProduct.imageUrlString当您遇到mainimagedidEndElement中的元素时,您必须为其赋值,即

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

  }
于 2013-02-07T08:22:18.347 回答
0

对于这种情况,我更喜欢更简单的 XML Parser 看看这个 http://vuknikolic.wordpress.com/2012/04/27/super-easy-ios-xml-parsing/

于 2013-02-07T08:27:17.773 回答
0

@Ravindra 给了你正确的答案,你需要以数组或其他方式管理你的数据。此 self.currentProduct.name = self.currentElementContent; 行也替换您的主要图像数据。所以最好使用字典结构

于 2013-02-07T09:59:04.623 回答