2013-08-18 64 views
0

我想解析一个我创建的简单xml文件。解析本地xml文件并在表格视图中显示数据

我通读了一堆关于这个主题的教程,但我仍然无法获得信息显示在模拟器上(我不断收到一个空列表)。

我的XML文件: 标题的第一本书 名称的第二本书的一个 标题 名称的两个

我的代码:

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
{ 
    NSString *xmlPath = [ [ NSBundle mainBundle ] pathForResource : @" Books.xml "  ofType:@"xml" ] ; 
    NSData *xmlData = [ NSData dataWithContentsOfFile : xmlPath ] ; 
    NSXMLParser *xmlParser = [ [ NSXMLParser alloc ] initWithData:xmlData ] ; 
    XMLParser *parser = [ [ XMLParser alloc ] initXMLParser ] ; 
    [ xmlParser setDelegate : parser ] ; 
    BOOL success = [xmlParser parse]; 
if (success) 
    NSLog (@" amount %i ", [ books count ]) ; 
else 
    NSLog(@"Error!!!"); 
    return YES; 
} 

@implementation XMLParser 

- (XMLParser *) initXMLParser 
{ 
    self = [ super init ] ; 
    appDelegate = (AppDelegate *) [ [ UIApplication sharedApplication ] delegate ] ; 
    return self ; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict 
{ 
    if ([ elementName isEqualToString : @" Books " ]) 
{ 
appDelegate.books = [ [ NSMutableArray alloc ] init ] ; 
} 
else if ([ elementName isEqualToString : @" Book " ]) 
{ 
aBook = [ [ Book alloc ] init ] ; 
aBook.bookID = [ [ attributeDict objectForKey : @" id " ] integerValue ] ; 
NSLog(@"Reading id value :%i", aBook.bookID); 
    } 
    else if ([ elementName isEqualToString : @" title " ]) 
    { 
     aBook.title = [[NSMutableString alloc] init]; 
    } 
    else if ([ elementName isEqualToString : @" author " ]) 
    { 
     aBook.author = [[NSMutableString alloc] init]; 
    } 
    NSLog (@" Processing Element : %@", elementName) ; 
} 

- (void) parser : (NSXMLParser *) parser foundCharacters : (NSString *) string 
{ 
    if (!currentElementValue) 
    { 
    currentElementValue = [ [ NSMutableString alloc ] initWithString : string ] ; 
    } 
    else 
    { 
    [ currentElementValue appendString : string ] ; 
    } 
    NSLog (@" Processing Value : %@ ", currentElementValue) ; 
} 

- (void) parser : (NSXMLParser *) parser didEndElement : (NSString *) elementName 
namespaceURI : (NSString *) namespaceURI qualifiedName : (NSString *) qName 

{ 
    if ([ elementName isEqualToString : @" Books " ]) 
    return; 
    if ([ elementName isEqualToString : @" Book " ]) 
    { 
    [ appDelegate.books addObject : aBook ] ; 
     aBook = nil ; 
    } 
    else 
    { 
    [ aBook setValue : currentElementValue forKey : elementName ] ; 
    } 
    currentElementValue = nil ; 
} 
@end 

@implementation MasterViewController 

    - (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void) viewDidLoad 
{ 
    [ super viewDidLoad ] ; 
    appDelegate = (AppDelegate *) [ [ UIApplication sharedApplication ] delegate ]  ; 
    self.title = @" Books ";  
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [ appDelegate.books count ] ; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"   forIndexPath:indexPath]; 
    aBook = [appDelegate.books objectAtIndex:indexPath.row]; 
    cell.textLabel.text = aBook.title; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     NSDate *object = _objects[indexPath.row]; 
     // [[segue destinationViewController] setDetailItem:object]; 
    } 
} 

@end 

谁能帮我找到我做错了什么?

+0

使用您的appdelegate全局变量的“垃圾场”,是一个贫穷的设计选择。定义和使用一个单独的模型类;为它添加一个单例(这里是[链接](http://stackoverflow.com/a/11945106/335858)解释如何做到这一点)。 – dasblinkenlight

+0

不是你不应该学习如何解析XML,而是你不能使用plist的原因? – Wain

+0

@dasblinkenlight,首先,感谢您的帮助,但我不明白如何利用您的建议。 不能使用appDelegate工作吗? – user1868558

回答

1

您几乎总是为您的字符串添加前导和尾随空格。我怀疑这是你想要的。

您也可以为文件路径执行此操作,并将.xml扩展名添加到资源名称,在使用pathForResource:ofType:时不应该这样做。

假设你的文件被命名为“Books.xml的”使用这个检索路径XML文件:

NSString *xmlPath = [[NSBundle mainBundle] pathForResource : @"Books" ofType:@"xml"]; 
+0

非常感谢你,模拟器上的列表仍然是空的,但现在我可以看到文件正在被解析。 有人告诉我要添加空格以使代码更具可读性,我认为我现在不会再这样做了。 我可以在Xcode的输出中看到xml文件中的元素正在被读取,只有我在模拟器上看不到任何东西。你有什么可以解决问题的建议吗? – user1868558

相关问题