0

我正在尝试解析我创建的一个简单的 xml 文件。

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

我的 xml 文件: 第一本书的标题 Name one 第二本书的标题 Name two

我的代码:

@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

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

4

1 回答 1

1

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

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

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

NSString *xmlPath = [[NSBundle mainBundle] pathForResource : @"Books" ofType:@"xml"];
于 2013-08-18T10:55:33.407 回答