将我的字典树从我的 xmlparser 类获取到我的rilldowntableappdelegate 文件
我似乎有一个可能是新手的问题,但就是这样。 如何从我的解析器类获取我的字典,该类已经为向下钻取表构建了一棵树,返回到我的钻取表委托类。 我的应用程序看起来像这样。
@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];
}
我一直从互联网上获得灵感和信息 有效的方法:
- 构建一个可以由我的应用程序读取的漂亮树(使用 writetoFile 进行测试)
- xmlparsing 有效(我体面的 xmlfile)
- drilldowntable 示例
结论:如何获取由我构建的(由解析器构建)树解析器到我的rilldowntableappdelegate?
附: 如果这不起作用,我也可以在我的委托文件中构建我的解析器(我知道很草率,但我认为我的填充树可以在那里工作)
I seem to have a probably newbie problem but here it is.
How do I get my dictionary from my parser class , which has building a tree for a drill down table back to my drilldowntabledelegate class.
my app looks something like this.
@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];
}
My parser file looks something like this
- (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];
}
I've been getting my inspiration and information from all over the internet
What works :
- Building a nice tree which can be read into by my app ( tested it with writetoFile)
- xmlparsing works ( I decent xmlfile )
- the drilldowntable example
conclusion: how do I get my (build by parser)Tree which has been build by my parser to my drilldowntableappdelegate ?
ps. if this doesn't work can I also build my parser in my delegate file ( i know sloppy but I assume my filled tree will work there )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了我的问题。
我通过在 drownappdelegate 文件中创建字典对象,然后通过解析器文件中的单例 UIApplication 使用它们来解决了我的问题。
I sovled my problem.
I solved my problem by creating the dictionary objects in my drilldownappdelegate file and then using them through the singleton UIApplication in my parser file.