【问题标题】:How to do selective parsing with NSXMLParser如何使用 NSXMLParser 进行选择性解析
【发布时间】:2012-01-02 16:59:51
【问题描述】:

我有以下 XML:

<data>
<title>Bookstore</title>
<site>www.bookstore.com</site>
<lastUpdate>11/17/2011</lastUpdate>
<books>
<unit>
<title>
Beginning iPhone 4 Development: Exploring the iOS SDK
</title>
<author>David Mark, Jack Nutting, Jeff LaMarche</author>
<isbn10>143023024X</isbn10>
<isbn13>978-1430230243</isbn13>
<pubDate>January 28, 2011</pubDate>
<price>23.99</price>
<description>
Beginning iPhone 4 Development is a complete course in iOS development. You'll master techniques that work on iPhone, iPad, and iPod touch. We start with the basics showing you how to download and install the tools you'll need, and how to create your first simple application. Next you'll learn to integrate all the interface elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders.
</description>
</unit>
<unit>...</unit>
<unit>...</unit>
</books>
<clothes>
<unit>
<title>T-Shirt</title>
<size>M</size>
<color>Red</color>
<price>10.99</price>
<description>
100% cotton T-shirt, wash in cold water with like colors
</description>
</unit>
<unit>...</unit>
<unit>...</unit>
<unit>...</unit>
</clothes>
<accessories>
<unit>
<title>Mug - Large</title>
<color>Black</color>
<price>6.99</price>
<description>
Large 16-ounce ceramic coffee mug, with witty use of IIT name on it.
</description>
</unit>
<unit>...</unit>
</accessories>
</data>

这是我得到的代码。我正在检查遇到的标签是否是“书”。

- (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:@"unit"]) {

        aBook = [[Book 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);

}

这里我试图将每个标签中的值分配给 Book 对象中的一个变量。

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if (qName) {
        elementName = qName;
    }       
    if (aBook) {            

    if ([elementName isEqualToString:@"title"]) {
        aBook.title = currentElementValue;
    } else if ([elementName isEqualToString:@"author"]) {
        aBook.author = currentElementValue;
    } else if ([elementName isEqualToString:@"isbn10"]) {
        aBook.isbn10 = currentElementValue;
    } else if ([elementName isEqualToString:@"isbn13"]) {
        aBook.isbn13 = currentElementValue;
    } else if ([elementName isEqualToString:@"pubDate"]) {
        aBook.pubDate = currentElementValue;

    } else if ([elementName isEqualToString:@"price"]) {
        aBook.price = currentElementValue;
    } else if ([elementName isEqualToString:@"description"]) {
        aBook.description = currentElementValue;
    }

        [appDelegate.books addObject:aBook];

        [aBook release];
        aBook = nil;    

    }

    [currentElementValue release];
    currentElementValue = nil;
}

我有一个 UITableView 来显示书籍列表,

Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
    [[cell textLabel] setText:[aBook title]];

问题是所有项目都显示在表格视图中,而不仅仅是书籍。如何将显示的项目限制为仅来自书籍。我认为我在 Parser DidStartElement 方法中出错了。

谢谢。

【问题讨论】:

    标签: objective-c ios4 nsxmlparser


    【解决方案1】:

    parser:didEndElement:namespaceURI:qualifiedName: 中,您需要检查&lt;/books&gt; 标签,然后忽略任何&lt;unit&gt; 标签。

    一种方法是在解析器中使用self.books 而不是appDelegate.books,然后仅当您到达&lt;/books&gt; 标记集appDelegate.booksself.books 然后将self.books 设置为零。如果您到达&lt;unit&gt; 标签并且self.books 为nil,那么您将忽略单位。

    【讨论】:

    • 现在我在 didStartElement 方法中有if([elementName isEqualToString:@"books"]) { //Initialize the array. self.books = [[NSMutableArray alloc] init]; },在 didEndElement 方法中有else if ([elementName isEqualToString:@"books"]) { [self.books addObject:aBook]; appDelegate.books = self.books; self.books =nil; }。这是你提到的吗?我试过了,我的 tableView 是空白的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2012-12-13
    相关资源
    最近更新 更多