【发布时间】: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