私は次のXML構造を持っています:
<product>
<id>3482</id>
<title>Product 1</title>
<number>1</number>
<description>Text</description>
</product>
<product>
<id>4743</id>
<title>Product 2</title>
<number>2</number>
<description>Text</description>
</product>
<product>
<id>3864</id>
<title>Product 3</title>
<number>3</number>
<description>Text</description>
</product>
すべてが解析され、NSMutableArrayに保存されます。ここで、キー「title」の値のみを取得したいと思います。しかし、NSMutableArrayを出力すると、次のようになります。
"<ChartListDataParserEntries: 0x1edf3c00>"
ChartListDataParserEntries.mで、解析されたXMLファイルの要素を管理しています。
私はすでに試しました:
for(ChartListDataParserEntries * CLDPE in self.parsedChartNew){}
しかし、これは私に1つの要素に対して1つの結果を与えるだけです。
では、どのようにしてすべての「タイトル」値(製品1、製品2、製品3 ...)をキャッチできますか?
編集
- (id)initWithData:(NSData *)data delegate:(id <ChartListParserDelegate>)theDelegate
{
self = [super init];
if (self != nil)
{
self.dataToParse = data;
self.delegate = theDelegate;
}
return self;
}
- (void)main
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.dataToParse];
[parser setDelegate:self];
[parser parse];
self.dataToParse = nil;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"chart"])
{
return;
}
else if ([elementName isEqualToString:@"new"])
{
self.fullyParsedChartNew = [NSMutableArray array];
chartListDataEntries = [[ChartListDataParserEntries alloc] init];
}
else if ([elementName isEqualToString:@"soon"])
{
self.fullyParsedChartSoon = [NSMutableArray array];
chartListDataEntries = [[ChartListDataParserEntries alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!self.parsingResults)
{
self.parsingResults = [NSMutableString stringWithString:string];
}
else
{
[self.parsingResults appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%@", parseError);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"chart"])
{
return;
}
else if ([elementName isEqualToString:@"new"])
{
[self.fullyParsedChartNew addObject:chartListDataEntries];
chartListDataEntries = nil;
}
else if ([elementName isEqualToString:@"soon"])
{
[self.fullyParsedChartSoon addObject:chartListDataEntries];
chartListDataEntries = nil;
}
else
{
[chartListDataEntries setValue:self.parsingResults forKey:elementName];
self.parsingResults = nil;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
if (![self isCancelled])
{
NSLog(@"%@", [self.fullyParsedChartNew valueForKeyPath:@"product_title"]);
[self.delegate didFinishParsing:self.fullyParsedChartNew andChart:self.fullyParsedChartSoon];
}
}
ChartListDataParserEntries.h
@interface ChartListDataParserEntries : NSObject
{
NSString *product_id;
NSString *product_title;
NSString *product_genre;
NSString *product_price;
NSString *product_stock;
NSString *product_stock_color;
NSString *product_stock_text;
UIImage *product_image;
}
@property (nonatomic, retain) NSString *product_id;
@property (nonatomic, retain) NSString *product_title;
@property (nonatomic, retain) NSString *product_genre;
@property (nonatomic, retain) NSString *product_price;
@property (nonatomic, retain) NSString *product_stock;
@property (nonatomic, retain) NSString *product_stock_color;
@property (nonatomic, retain) NSString *product_stock_text;
@property (nonatomic, retain) UIImage *product_image;
@end