如何同时解析两个不同的.xml文件?

时间:2011-08-06 00:37:48

标签: ios parsing nsmutablearray nsxmlparser

我正在使用标签栏应用程序,有4种不同的表视图。有四个标签栏项目,每个项目都有不同的表格视图。我应该填充.xml个文件的数组。然而,问题是:

我一次只能解析一个XML文件。如何通过.xml同时解析两个或多个NSXMLParser个文件?

或者我应该合并xml files? Yet, if I merge, I have to create two or more NSMutableArray以将它们放入另一个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 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解了您的问题,因此请尝试提供更多详细信息,如果需要,我会编辑我的答案。

对于要解析的每个xml文件,您应该有一个单独的NSXMLParser实例。在您的情况下,标签栏上的每个视图控制器中可能应该有一个NSXMLParser实例。

即使对所有这些NSXMLParserDelegate使用相同的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实例)设置为视图控制器上的属性,并在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
    }
}