如何通过xml解析器解析此xml文件中的属性

时间:2012-06-04 06:52:37

标签: iphone xml-parsing

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);
}

1 个答案:

答案 0 :(得分:3)

首先拿一个bool变量。然后编写以下代码。也许它会帮助你我几天前完成了相关的工作。

首先.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解析器委派方法

pragma mark - 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]);
    }
}

注意:我已经检查过,它正在运行,您可以在代码中使用它,如果它仍然不起作用,那么项目中会出现其他问题,您可以通过单独的项目来验证它。