解析本地xml文件并在表视图中显示数据

时间:2013-08-18 10:16:20

标签: ios xml-parsing nsxmlparser

我正在尝试解析我创建的一个简单的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

任何人都可以帮我找出我做错的事吗?

1 个答案:

答案 0 :(得分:1)

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

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

假设您的文件名为“Books.xml”,请使用此方法检索XML文件的路径:

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