リモート Web サーバーから XML ファイルを正常に解析しました。私の XML には、画像リンクを含むいくつかの要素が含まれています。私は以下の方法で試しましたが、要素のすべての画像を取得しているため失敗しました。このため、特定の UIImageView に特定の画像を割り当てることはできません。誰でも私にこれを行う方法を提案できますか、
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 解析
- (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;
}
}
View Controllerでは、要素の画像を取得するのに疲れたときに、XMLMain
の要素のみを取得しています。Category
ビューコントローラー
[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];
}