我真的很陌生,而且我正在尝试阅读RSS Feed,到目前为止一切都很好。我就是这样做的:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];
[item setObject:currentImage forKey:@"enclosure"];
[stories addObject:[item copy]];
NSLog(@"adding story: %@", currentTitle);
}
}
问题在于圈地。在XML中看起来像这样:
<enclosure length="150" url="urltoimage.jpg" type="image/jpeg" />
如何从我的函数中获取该元素的 url 属性?
编辑 XML如下所示:
<item>
<title>...</title>
<link>...</link>
<description>...</description>
<pubDate>...</pubDate>
<enclosure length="150" url="urltoimage.jpg" type="image/jpeg" />
</item>
我的foundCharacters函数如下所示:
- (void)parser: (NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
答案 0 :(得分:12)
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
// just do this for item elements
if(![elementName isEqual:@"enclosure"])
return;
// then you just need to grab each of your attributes
NSString * name = [attributeDict objectForKey:@"url"];
// ... get the other attributes
// when we have our various attributes, you can add them to your arrays
[m_NameArray addObject:name];
// ... same for the other arrays
}
答案 1 :(得分:6)
您必须再处理一个跟踪属性的回调:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
attributeDict
是包含所有找到的元素参数的字典