0
NSDictionaryのNSArrayに対するXMLの解析に問題があります。理由は分かりませんが、配列の代わりに2つのオブジェクト(この場合)を取得すると、2つのオブジェクトが等しいデータで取得されます...なぜですか?NSDictionaryのNSArrayにXMLを解析する - エラー - なぜですか?
@interface RLparseXMLToArrayOfDictionarys : NSObject <NSXMLParserDelegate> {
NSMutableArray *arrayWithResult;
NSMutableDictionary *tempDict;
NSMutableString *currentString;
NSString *groupKey;
}
@property (nonatomic, strong) NSString *groupKey;
@property (nonatomic, retain) NSMutableArray *arrayWithResult;
@property (nonatomic, retain) NSMutableDictionary *tempDict;
-(NSArray *)parseXMLWithStringToArray:(NSString *)stringWithXML withGroupKey:(NSString *)groupKeyToIgnore;
@end
@implementation RLparseXMLToArrayOfDictionarys
@synthesize groupKey;
@synthesize arrayWithResult;
@synthesize tempDict;
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
-(NSArray *)parseXMLWithStringToArray:(NSString *)stringWithXML withGroupKey:(NSString *)groupKeyToIgnore{
NSData *currentStringData = [stringWithXML dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:currentStringData];
[parser setDelegate:self];
// Set Parser Options
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
//key to ignore
self.groupKey = groupKeyToIgnore;
if (!arrayWithResult) {
arrayWithResult = [[NSMutableArray alloc] init];
}
if (!tempDict) {
tempDict = [[NSMutableDictionary alloc] init];
}
[parser parse];
NSLog(@"return: %@", arrayWithResult);
return arrayWithResult;
}
#pragma mark -
#pragma mark XML methods
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(!currentString){
currentString = [[NSMutableString alloc] init];
}
[currentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSString *currentStringNoWhiteSpace = [currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([elementName isEqualToString:groupKey]){
[arrayWithResult addObject:tempDict];
// [tempDict removeObjectsForKeys:[tempDict allKeys]];
}
else if (currentStringNoWhiteSpace != nil)
[tempDict setValue:currentStringNoWhiteSpace forKey:elementName];
currentStringNoWhiteSpace = nil;
currentString = nil;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
}
@end