开发者

How to parse two different .xml files at the same time?

I am using a tab bar application, and there are 4 different table views. There are four tab bar items and each has a different table view. I should populate arrays form .xml files. Yet, problem is this:

I can parse only one XML file at a time. How can I parse two or more .xml files at the same time by NSXMLParser?

Or should I merge the xml files? Yet, if I merge, I have to create two or moreNSMutableArray`s to put those into another tableview views. Any suggestion?

What do you guys suggest? I do not know how to merge those xml files, but even if I do, I should create NSMutableArrays to use those to populate arrays for each view.

Thanks in advance.

Edit:

This is my 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

This is my 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

This is my 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


I'm not sure I understood your question correctly, so please try to provide more details and I'll edit my answer if needed.

You should have a separate NSXMLParser instance for every xml file you want to parse. In your case there should probably be one NSXMLParser instance in each of the view controllers on your tab bar.

You can start them all even with the same NSXMLParserDelegate for all of them because all delegate methods tell you which NSXMLParser instance called them.

Edit:

You should move this code to the view controllers on your tab bar controller:

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

except this line (keep it in appdelegate and pass a reference to it into all the view controllers on your tab bar controller that need to parse something):

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

Then set a parser (the NSXMLParser instance) as a property on your view controllers and inside xmlParse check wich of the parsers called the delegate and act accordingly. For example:

- (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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜