2011-08-06 2 views
0

私はタブバーアプリケーションを使用していますが、4つのテーブルビューがあります。 4つのタブバー項目があり、それぞれ異なる表ビューがあります。私は.xmlファイルを配列に取り込みます。しかし、問題は次のとおりです。2つの異なる.xmlファイルを同時に解析するには?

私は一度に1つだけXMLファイルを解析できます。 NSXMLParserで2つ以上のファイルを同時に解析するにはどうすればよいですか?

また、xml files? Yet, if I merge, I have to create two or more NSMutableArray`sをマージして、それらを別のtableviewビューに入れるべきですか?なにか提案を?

あなたは何をお勧めしますか?私はそれらのxmlファイルをマージする方法はわかりませんが、私が行っても、それらを使って各ビューの配列を取り込むためにNSMutableArrayを作成する必要があります。

ありがとうございます。

編集:

これは私のAppDelegate.m

@synthesize window = _window; 
@synthesize tabBarController = _tabBarController; 


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

//Initialize the delegate. 
XMLParser *parser = [[XMLParser alloc] initXMLParser]; 

// Configure and show the window 
self.window.rootViewController = self.tabBarController; 
[self.window makeKeyAndVisible]; 
[window makeKeyAndVisible]; 
} 


- (void)applicationWillTerminate:(UIApplication *)application { 
// Save data if appropriate 
} 


- (void)dealloc { 
[tabBarController release]; 
[window release]; 
[super dealloc]; 
} 

@end 

である。これは私のXMLparser.m次のとおりです。

#import "XMLParser.h" 
#import "XMLAppDelegate.h" 
#import "Duyuru.h" 
#import "Beste.h" 
#import "BesteViewController.h" 
#import "DuyuruViewController.h" 

@implementation XMLParser 

- (XMLParser *) initXMLParser { 

[super init]; 

appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate]; 

return self; 
} 

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


if (parser == bestevc.parser) { 

    if([elementName isEqualToString:@"Besteler"]) { 
    //Initialize the array. 
    bestevc.besteler = [[NSMutableArray alloc] init]; 
    } 

    else if([elementName isEqualToString:@"Beste"]) { 

     //Initialize the book. 
     aBeste = [[Beste alloc] init]; 

     //Extract the attribute here. 
     aBeste.besteID = [[attributeDict objectForKey:@"id"] integerValue]; 

     NSLog(@"Reading id value :%i", aBeste.besteID); 
    } 

} 

if (parser == duyuruvc.parser ) { 

    if([elementName isEqualToString:@"Duyurular"]) { 
     //Initialize the array. 
     duyuruvc.duyurular = [[NSMutableArray alloc] init]; 
    } 

    else if([elementName isEqualToString:@"Duyuru"]) { 

     //Initialize the book. 
     aDuyuru = [[Duyuru alloc] init]; 

     //Extract the attribute here. 
     aDuyuru.duyuruID = [[attributeDict objectForKey:@"id"] integerValue]; 

     NSLog(@"Reading id value :%i", aDuyuru.duyuruID); 
    } 

    } 

} 

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

if (parser == bestevc.parser ) { 
    if(!currentElementValue1) 
     currentElementValue1 = [[NSMutableString alloc] initWithString:string]; 
    else 
     [currentElementValue1 appendString:string]; 

    NSLog(@"Processing Value: %@", currentElementValue1); 
} 

if (parser == duyuruvc.parser ) { 
    if(!currentElementValue2) 
     currentElementValue2 = [[NSMutableString alloc] initWithString:string]; 
    else 
     [currentElementValue2 appendString:string]; 

    NSLog(@"Processing Value: %@", currentElementValue2); 
} 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

    if (parser == bestevc.parser) { 

    if([elementName isEqualToString:@"Besteler"]) 
    return; 
    //There is nothing to do if we encounter the Books element here. 

    // and release the object. 
    if([elementName isEqualToString:@"Beste"]) { 
     [bestevc.besteler addObject:aBeste]; 

     [aBeste release]; 
     aBeste = nil; 
    } 
    else 
     [aDuyuru setValue:currentElementValue1 forKey:elementName]; 

    [currentElementValue1 release]; 
    currentElementValue1 = nil; 
} 
if (parser == duyuruvc.parser) { 

    if([elementName isEqualToString:@"Duyurular"]) 
     return; 
    //There is nothing to do if we encounter the Books element here. 

    // and release the object. 
    if([elementName isEqualToString:@"Duyuru"]) { 
     [duyuruvc.duyurular addObject:aDuyuru]; 

     [aDuyuru release]; 
     aDuyuru = nil; 
    } 
    else 
     [aDuyuru setValue:currentElementValue2 forKey:elementName]; 

    [currentElementValue2 release]; 
    currentElementValue2 = nil; 
} 

} 


- (void) dealloc { 
[aDuyuru release]; 
[aBeste release]; 
[currentElementValue1 release]; 
[currentElementValue2 release]; 
[super dealloc]; 
} 

@end 

これは私のBesteViewController.mです:

#import "BesteViewController.h" 
#import "XMLAppDelegate.h" 
#import "Beste.h" 
#import "XMLParser.h" 

@implementation BesteViewController 
@synthesize parser, besteler; 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) 
section { 
    return [besteler count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 
(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
    reuseIdentifier:CellIdentifier] autorelease]; 
} 

Beste *aBeste = [besteler objectAtIndex:indexPath.row]; 

[[cell textLabel] setText:aBeste.name]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

// Set up the cell 
return cell; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Uncomment the following line to add the Edit button to the navigation bar. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem; 

appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSURL *url = [[NSURL alloc] 
     initWithString:@"https://sites.google.com/site/bfbremoteser 
    ver/iphoneapp/besteler.xml"]; 
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 


//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!!!"); 

self.navigationItem.title = @"Besteler"; 
} 







/* 
// Override to support rearranging the list 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath 

*)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


/* 
// Override to support conditional rearranging of the list 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*) 
indexPath { 
// Return NO if you do not want the item to be re-orderable. 
return YES; 
} 
*/ 

/* 
- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
} 
*/ 
/* 
- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 
} 
    */ 
/* 
- (void)viewWillDisappear:(BOOL)animated { 
} 
*/ 
/* 
- (void)viewDidDisappear:(BOOL)animated { 
} 
*/ 




- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
// Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
[besteler release]; 
[appDelegate release]; 
[super dealloc]; 
} 


@end 

答えて

1

私はあなたの質問を正しく理解しているかどうか分からないので、詳細を提供してください。必要に応じて回答を編集してください。

解析するすべてのXMLファイルに対して、別のNSXMLParserインスタンスが必要です。あなたのケースでは、おそらく1つのNSXMLParserのインスタンスがそれぞれのビューコントローラのタブバーにあるはずです。

すべてのデリゲートメソッドがそれらを呼び出すNSXMLParserインスタンスを通知するので、すべて同じNSXMLParserDelegateでそれらをすべて開始することもできます。

編集:

あなたのタブバーコントローラのビューコントローラにこのコードを移動する必要があります。この行を除く

NSURL *url = [[NSURL alloc] initWithString:@"..."]; 
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 


//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!!!"); 

を(appdelegateでそれを維持し、すべてのビューにそれへの参照を渡します何かを解析する必要があなたのタブバーコントローラ)上のコントローラ:

XMLParser *parser = [[XMLParser alloc] initXMLParser]; 

その後は、あなたのビューコントローラのプロパティとしてパーサ(NSXMLParserインスタンス)を設定しましたsと内部のxmlParseチェックでパーサーがデリゲートと呼ばれ、それに応じて動作します。たとえば、

+0

正しく理解しました。しかし、別のNSXMLパーサーを使用する例は見つけられませんでした。詳細を教えたり、チュートリアルやxcodeprojを私に指示したりできますか?また、私の場合、1つのNSXMLパーサーが存在し、1つの.xmlファイルを意味すると述べました。だから私はXMLファイルを結合するとき、どのように私は1つのXMLファイルから複数の配列を作成し、別のビューにオブジェクトを追加することができますか? Thx –

+0

編集:XMLに複数の要素を使用すると、デリゲートパーサーのdidStartElementメソッドを開始する必要がありますか?または、すべての要素を1つのdidStartElementメソッドで読み取ることはできますか? –

+0

これまでのコードを共有することができれば、それを編集できますが、プロジェクト全体を一から作成する時間がありません。 –

関連する問題