从我的xmlparser类获取我的字典树..到我的drilldowntableappdelegate文件

时间:2009-07-19 14:56:42

标签: objective-c nsmutabledictionary xml-parsing

我似乎有一个可能是新手的问题,但现在是。 我如何从我的解析器类中获取我的字典,该解析器类已经为下钻表构建了一个树,返回到我的drilldowntabledelegate类。 我的应用程序看起来像这样。

@implementation DrillDownAppAppDelegate

@synthesize window;

@synthesize navigationController;

@synthesize data;

-(void)StartParser
{   
    //NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/virtuagymtestxml/testxml/Books.xml"];
    //NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    //local
    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *DataPath = [ Path stringByAppendingPathComponent:@"exercises.xml"];
    NSData *Data = [[NSData alloc] initWithContentsOfFile:DataPath];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:Data];
    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

    if(success){
        NSLog(@"No Errors");
    }
    else
    {
        NSLog(@"Error Error Error!!!"); 
    }

}


- (void)applicationDidFinishLaunching:(UIApplication *)application {

        [self startParser];

    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *DataPath = [Path stringByAppendingPathComponent:@"Tree2.plist"];

    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];


         //this is what needs to happen
         **NSDictionary *tempDict = dictionaryFromMyParser**

    self.data = tempDict;
    [tempDict release];

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];

}

我的解析器文件看起来像这样

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"exercises"]) {

      //init tree 
        Tree = [NSMutableDictionary new];
        Rows = [NSMutableArray new];
        [Tree setObject:Rows forKey:@"Rows"];

        //Initialize the array.
             ... all kinds of arrays here 
    }

if([elementName isEqualToString:@"menu_mob"]) {
        amenuMob = [[menuMob alloc]init];
    }

    if([elementName isEqualToString:@"menuitem"]) {
        amenuItem = [[menuItem alloc] init];
        amenuItem.IDE = [[attributeDict objectForKey:@"id"] integerValue];
        amenuItem.text = [attributeDict objectForKey:@"text"];

        NSLog(@"reading id menuitem %i", amenuItem.IDE);
        NSLog(@"reading text menuitem %@", amenuItem.text);
        [appDelegate.menuitems addObject:amenuItem];
}

我一直在从互联网上获取灵感和信息 什么有效:

  1. 构建一个可以通过我的应用程序读取的漂亮树(用writetoFile测试)
  2. xmlparsing的作品(我体面的xmlfile)
  3. 可钻取的示例
  4. 结论:如何通过我的解析器为我的drilldowntableappdelegate构建我的(由解析器构建)树?

    PS。如果这不起作用,我还可以在我的委托文件中构建我的解析器(我知道马虎但我认为我的填充树将在那里工作)

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。 我通过在drilldownappdelegate文件中创建字典对象,然后通过解析器文件中的单例UIApplication使用它们解决了我的问题。