0

XML 文件代码:

  <Books>
  <Book id="1" isbn="123456">
  <name>Let Us C</name>
  <authors>
  <author>Yashwant Kanetkar</author>
  <author>ABC</author>
  </authors>
  <price>250.00</price>
  </Book>
  <Book id="2" isbn="345678">
  <name>Programing With C++</name>
  <authors>
  <author>Balaguruswamy</author>
  <author>XYZ</author>
  </authors>
  <price>400.00</price>
  </Book>
  <Book id="3" isbn="789012">
  <name>Professional iPhone and iPad Application Development</name>
  <authors>
  <author>Gene Backlin</author>
  <author>QWE</author>
  </authors>
  <price>550.00</price>
  </Book>
  <Book id="4" isbn="901234">
  <name>Beginning iOS 4 Application Development</name>
  <authors>
  <author>Wei-Meng Lee</author>
  <author>IOP</author>
  </authors>
  <price>500.00</price>
  </Book>
  </Books>

谁能帮我解决这个问题?

我正在使用这段代码:

- (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];
        aBook.isbn = [[attributeDict objectForKey:@"isbn"] integerValue];
        NSLog(@"Reading isbn value : %@", aBook.isbn);
        NSLog(@"Reading id value :%i", aBook.bookID);
    }
    NSLog(@"Processing Element: %@", elementName);
}
4

1 回答 1

3

首先取一个布尔变量。然后编写以下代码。也许它会对你有所帮助,我几天前已经完成了相关工作。

第一个 .h 文件

@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
    NSMutableArray *_idArray;
    NSMutableArray *_isbnArray;
    BOOL isBook;
}

然后在 .m 文件中你应该写这个。

//在ViewDidLoad中

- (void)viewDidLoad
{
    [super viewDidLoad];
    _idArray = [[NSMutableArray alloc] init];
    _isbnArray = [[NSMutableArray alloc] init];
    
    NSString *xmlString = [[NSBundle mainBundle] pathForResource:@"today_in_history_short" ofType:@"xml"];
    NSURL * fileURL = [NSURL fileURLWithPath:xmlString];
    NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
    [parser setDelegate:self];
    [parser parse];
}

// 现在在 xml 解析器委托方法中

杂注标记 - NSXMLParser 委托方法

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    
    if ([elementName isEqualToString:@"Books"]) {
        isBook = YES;
    }
    else if ([elementName isEqualToString:@"Book"] && isBook){
        NSString *idString = [attributeDict objectForKey:@"id"];
        NSString *isbnString = [attributeDict objectForKey:@"isbn"];
        [_idArray addObject:idString];
        [_isbnArray addObject:isbnString];
        NSLog(@"Book id is: %@ and Book isbn is: %@",idString,isbnString);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    
    if ([elementName isEqualToString:@"Books"]) {
        isBook=NO;
        NSLog(@"Id Array count is :%d",[_idArray count]);
    }
}

注意:我已经检查过了,它正在工作,你可以在你的代码中使用它,如果它仍然不能工作,那么你的项目中会有其他问题,你可以通过制作单独的项目来验证它。

于 2012-06-04T07:28:51.527 回答