tbl*_*ert 3 xcode nsxmlparser ios5
我是新手,我正在尝试阅读RSS提要,到目前为止一切都很好.我是这样做的:
- (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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题在于圈地.在XML中看起来像这样:
<enclosure length="150" url="urltoimage.jpg" type="image/jpeg" />
Run Code Online (Sandbox Code Playgroud)
如何从我的函数中获取该元素的url属性?
编辑 XML看起来像这样:
<item>
<title>...</title>
<link>...</link>
<description>...</description>
<pubDate>...</pubDate>
<enclosure length="150" url="urltoimage.jpg" type="image/jpeg" />
</item>
Run Code Online (Sandbox Code Playgroud)
我的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];
}
}
Run Code Online (Sandbox Code Playgroud)
Ten*_*kar 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
}
Run Code Online (Sandbox Code Playgroud)
您必须再处理一个回调以跟踪属性:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
Run Code Online (Sandbox Code Playgroud)
attributeDict 是包含所有找到的元素参数的字典
| 归档时间: |
|
| 查看次数: |
5598 次 |
| 最近记录: |