0

这个问题可能经常被问到,但我几乎阅读了所有这些问题,但无法为我的案例找到解决方案。我计划将解析后的数据保存到 NSMutableDictionary。解析器工作正常,如果我得到一个日志,它会显示所有已解析的数据。问题是最后一项仅保存在 NSDictionary 中。我可以猜测我将字典放在错误的方法中,但我无法找到更好的保存解决方案。我使用字典来保存元素的名称和文本。这是我的代码:

@implementation Parser
@synthesize currentElementPointer, rootElement;

@synthesize dictionary;

-(id)initParser
{
    if(self = [super init]) {
        tvc = (TimeTableViewController*)[[UIApplication sharedApplication]delegate];

        APUAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        context = [appDelegate managedObjectContext];

        [self deleteAllObjects:@"TimeTable"];
    }
    return self;
}


#pragma mark -
#pragma mark PARSER

-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.parsing = YES;
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"intake"])
    {
        NSString *name = attributeDict[@"name"];
        self.parsing = [name isEqualToString:[self sendIntakeToParser]];
    }
    if(![self isParsing]) return;

    if(self.rootElement == nil) {
        self.rootElement = [[List alloc]init];
        self.currentElementPointer = self.rootElement;
    } else {
        List *newList = [[List alloc]init];
        newList.parent = self.currentElementPointer;
        [self.currentElementPointer.subElements addObject:newList];
        self.currentElementPointer = newList;
    }

    self.currentElementPointer.name = elementName;
    self.currentElementPointer.attributes = attributeDict;

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    if(![self isParsing]) return;

    if([self.currentElementPointer.text length] > 0) {
        self.currentElementPointer.text = [self.currentElementPointer.text stringByAppendingString:string];
    } else {
        self.currentElementPointer.text = string;
    }


    dictionary = [[NSMutableDictionary alloc]init];
    [dictionary setObject:[self.currentElementPointer text] forKey:[self.currentElementPointer name]];

}


-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([self isParsing])
    {
        self.currentElementPointer = self.currentElementPointer.parent;
    } else if ([elementName isEqualToString:@"intake"])
    {
        self.parsing = YES;
    }
}

这是我的 xml 结构: 在此处输入图像描述

4

1 回答 1

1

您的问题的根源是您在每次调用时重新实例化您的字典,每个parser:foundCharacters:具有文本的元素将至少调用一次。您可以做的一件事是将字典实例化移动到parser:didStartElement:...每当elementName等于 时@"timetable",在其中实例化一个可变数组,parser:didStartDocument:然后在 中parser:didEndElement:...,如果 elementName 是 `@"timetable",则将其添加到您的数组中。

这方面的一个例子如下所示:

解析器.h:

#import <Foundation/Foundation.h>

@interface Parser : NSObject <NSXMLParserDelegate>

- (void)parseData:(NSData *)data;

@end

解析器.m:

#import "Parser.h"

@interface Parser ()

@property (nonatomic,strong) NSMutableArray *timetableDictionaries;
@property (nonatomic,strong) NSMutableDictionary *currentTimetableDictionary;
@property (nonatomic,copy) NSString *currentElementName;

@end

@implementation Parser

- (void)parseData:(NSData *)data
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.timetableDictionaries = [NSMutableArray array];
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    self.currentElementName = elementName;
    if ([elementName isEqualToString:@"timetable"]) {
        self.currentTimetableDictionary = [NSMutableDictionary dictionary];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.currentElementName) {
        NSString *existingCharacters = self.currentTimetableDictionary[self.currentElementName] ? : @"";
        NSString *nextCharacters = [existingCharacters stringByAppendingString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
        if ([nextCharacters length] > 0) {
            self.currentTimetableDictionary[self.currentElementName] = nextCharacters;
        }
    }
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"timetable"]) {
        [self.timetableDictionaries addObject:self.currentTimetableDictionary];
        self.currentTimetableDictionary = nil;
    }
    self.currentElementName = nil;
}


- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"timetable dictionaries = %@",self.timetableDictionaries);
}

@end

我用以下文件测试了上面的类:

<weekof week="2013-07-29">
    <intake name="APCF1304">
        <timetable>
            <date>SOME DATE</date>
            <time>SOME TIME</time>
            <lecturer>SOME LECTURER</lecturer>
        </timetable>
        <timetable>
            <date>ANOTHER DATE</date>
            <time>ANOTHER TIME</time>
            <lecturer>ANOTHER LECTURER</lecturer>
        </timetable>
    </intake>
</weekof>

它给出了以下输出:

2013-08-11 11:50:03.205 MyParser[25368:c07] timetable dictionaries = (
        {
        date = "SOME DATE";
        lecturer = "SOME LECTURER";
        time = "SOME TIME";
    },
        {
        date = "ANOTHER DATE";
        lecturer = "ANOTHER LECTURER";
        time = "ANOTHER TIME";
    }
)

但是,您已经构建了文档树,可能是出于其他目的,因此您可以在解析结束时重新使用它来构建字典。例如,如果你只想要每个时间表的字典,你可以用伪代码做一些看起来像这样的事情:

dictionaries = [new mutable array]
element = self.root
[extract dictionaries from: element to:dictionaries]

implementation of extractDictionariesFrom:element to:dictionaries:
    if any subelement has non-nil text:
        item = [new mutable dictionary]
        for each sub element:
            item[element name] = element text
        [add item to dictionaries]
    else
        for each subelement:
            [extract dictionaries from: subelement to: dictionaries]

然后,您可以在解析器回调中放弃使用可变字典。

于 2013-08-11T10:53:21.620 回答