2011-08-06 51 views
0

我正在使用一個標籤欄應用程序,並且有4個不同的表格視圖。有四個標籤欄項目,每個都有不同的表格視圖。我應該填充數組表格.xml文件。然而,問題是這樣的:如何同時解析兩個不同的.xml文件?

我一次只能解析一個XML文件。如何通過NSXMLParser同時解析兩個或更多.xml文件?

或者我應該合併xml files? Yet, if I merge, I have to create two or more NSMutableArray的把它們放到另一個tableview視圖。任何建議?

你們有什麼建議?我不知道如何合併這些xml文件,但即使我這樣做,我也應該創建NSMutableArray s來使用它們爲每個視圖填充數組。

在此先感謝。

編輯:

這是我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實例。在你的情況下,你的標籤欄上的每個視圖控制器中可能應該有一個NSXMLParser實例。

即使使用相同的NSXMLParserDelegate,也可以全部啓動它們,因爲所有委託方法都會告訴您哪個NSXMLParser實例調用它們。

編輯:

您應該將此代碼移動到視圖控制器的標籤欄控制器上:

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檢查稱爲委託的解析器,並據此採取行動。例如:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict { 
    if (parser == firstViewController.parser) { 
     // first tab called this parser delegate method, insert code to parse it here 
    } else if (parser == firstViewController.parser) { 
     // second...and so on 
    } 
} 
+0

你理解正確。然而,我找不到任何使用單獨的NSXML解析器的例子,你能給我更多的細節還是指導我使用教程或xcodeproj?另外,你說在我的情況下,應該有一個NSXMLParser,而你的意思是一個.xml文件。所以,當我結合XML文件,我怎麼能從一個XML文件創建多個數組,並添加對象到單獨的視圖? Thx –

+0

編輯:如果我在我的xml中使用多個元素,我應該啓動委託解析器didStartElement方法嗎?或者我可以通過一個didStartElement方法讀取所有元素嗎? –

+0

如果您可以共享您到目前爲止的代碼,我可以爲您編輯該代碼,但我沒有時間從頭開始創建整個項目。 –

相關問題