2013-08-18 63 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

相關問題