我正在使用一个标签栏应用程序,并且有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
你理解正确。然而,我找不到任何使用单独的NSXML解析器的例子,你能给我更多的细节还是指导我使用教程或xcodeproj?另外,你说在我的情况下,应该有一个NSXMLParser,而你的意思是一个.xml文件。所以,当我结合XML文件,我怎么能从一个XML文件创建多个数组,并添加对象到单独的视图? Thx –
编辑:如果我在我的xml中使用多个元素,我应该启动委托解析器didStartElement方法吗?或者我可以通过一个didStartElement方法读取所有元素吗? –
如果您可以共享您到目前为止的代码,我可以为您编辑该代码,但我没有时间从头开始创建整个项目。 –