2011-08-06 3 views
0

나는 탭 막대 응용 프로그램을 사용하고 있으며 4 가지 테이블보기가 있습니다. 네 개의 탭 표시 줄 항목이 있으며 각각 다른 테이블보기가 있습니다. 배열을 채우려면 .xml 파일을 사용해야합니다. 그러나 문제는 다음과 같습니다.두 개의 다른 .xml 파일을 동시에 구문 분석하는 방법은 무엇입니까?

한 번에 하나의 파일 XML 만 구문 분석 할 수 있습니다. 두 개 이상의 .xml 파일을 동시에 NSXMLParser 개까지 구문 분석하려면 어떻게해야합니까?

또는 xml files? Yet, if I merge, I have to create two or more NSMutableArray`s를 다른 테이블 뷰 뷰에 병합해야합니까? 어떠한 제안?

당신은 무엇을 제안합니까? 나는 그 XML 파일을 병합하는 방법을 모르지만, 비록 내가 할지라도, 나는 각보기에 대해 배열을 채우기 위해 이들을 사용하기 위해 NSMutableArray을 생성해야한다.

미리 감사드립니다.

편집 : 내

@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 

AppDelegate.m 내는 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 인스턴스가 있어야합니다.

모든 대리자 메서드가 NSXMLParser 인스턴스를 호출했기 때문에 모든 NSXMLParserDelegate를 사용하여 모든 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를 통해 직접 지시 할 수 있습니까? 또한, 내 경우에는 하나의 NSXMLParser가 있어야하며 하나의 .xml 파일을 의미한다고 말했습니까? 그래서 XML 파일을 결합하면 어떻게 하나의 XML 파일에서 여러 배열을 만들고 별도의 뷰에 객체를 추가 할 수 있습니까? Thx –

+0

편집 : 내 XML에서 둘 이상의 요소를 사용하는 경우 대리자 파서 didStartElement 메서드를 시작해야합니까? 또는 하나의 didStartElement 메소드로 모든 요소를 ​​읽을 수 있습니까? –

+0

지금까지 작성한 코드를 공유 할 수 있다면 편집 할 수는 있지만 처음부터 전체 프로젝트를 만들 시간이 없습니다. –

관련 문제