【问题标题】:parse XML to NSArray of NSDictionary - error - why?将 XML 解析为 NSDictionary 的 NSArray - 错误 - 为什么?
【发布时间】:2011-07-29 20:48:37
【问题描述】:

我在将 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

【问题讨论】:

    标签: nsarray nsdictionary nsxmlparser


    【解决方案1】:

    完成!

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

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多