如何通过xml解析器解析这个xml文件中的属性 [英] how to parse the attributes like in this xml file through xml parser

查看:23
本文介绍了如何通过xml解析器解析这个xml文件中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

谁能帮我解决这个问题?

Can anyone pls help me with this?

我正在使用此代码:

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

推荐答案

先取一个bool变量.然后编写以下代码.也许对你有帮助,我几天前已经完成了相关工作.

First take a bool variable. and then write the following code. perhaps it will help you I have done the related work , a few days ago.

第一个 .h 文件

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

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

Then in .m file you should write this.

//在ViewDidLoad中

//In 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 解析器委托方法中

// Now In xml Parser Delegate Methods

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

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

Note: I have check it , It's working , you can use it in you code , if still it's won't working then there will be something else wrong in your project, you can verify it by making separate project.

这篇关于如何通过xml解析器解析这个xml文件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆