我有以下 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、产品 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