将XML解析为NSDictionary的NSArray - 错误 - 为什么?

时间:2011-07-29 20:48:37

标签: nsarray nsdictionary nsxmlparser

我在解析XML到NSDictionary的NSArray时遇到问题。我不知道为什么,但是代替数组得到2个对象(在这种情况下),它需要2个具有相同数据的对象......为什么?

以下是代码:

@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

1 个答案:

答案 0 :(得分:0)

完成!

替换[tempDict removeObjectsForKeys:[tempDict allKeys]];tempDict = [[NSMutableDictionary alloc] init];

:)