[英]NSXMLParser Parsing Attributes
How do you extract attributes from XML using NSXML parser??如何使用 NSXML 解析器从 XML 中提取属性?
Heres my xml:这是我的 xml:
<item>
<title>Button hails 'amazing' win</title>
<description>Jenson Button hailed yesterday's crazy Canadian Grand Prix victory as the best of his Formula One career.
</description>
<link>http://www.skysports.com/story/0,19528,12433_6986809,00.html</link>
<guid isPermaLink="false">12433_6986809</guid>
<pubDate>Mon, 13 Jun 2011 06:21:00 GMT</pubDate>
<category>News Story</category>
<enclosure type="image/jpg" url="http://img.skysports.com/11/06/128x67/Canadian-GP-Jenson-Button-celebrates1_2609288.jpg" length="123456" />
</item>
I need to get the url from the enclosure tags.我需要从附件标签中获取 url。
Thanks谢谢
if([elementName isEqualToString:@"enclosure"])
{
NSString *urlValue=[attributeDict valueForKey:@"url"];
NSString *urlValue=[attributeDict valueForKey:@"type"];
NSString *urlValue=[attributeDict valueForKey:@"length"];
}
you need to use NSXMLParser and its delegate functions您需要使用 NSXMLParser 及其委托函数
- -
(BOOL) parse:(NSData *)xmlData
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
then you need to use some thing like this那么你需要使用这样的东西
if([elementName isEqualToString:@"enclosure"])
{
NSMutableDictionary *Dict=[NSMutableDictionary dictionary];
[Dict setObject:[attributeDict valueForKey:@"url"] forKey:@"url"];
[categoryDict setObject:[attributeDict valueForKey:@"type"] forKey:@"type"];
}
The method...方法...
(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
Gives you a dictionary of attributes and their keys (attributeDict)... look for an entry keyed "url" when the elementName is equal to "enclosure"...为您提供属性及其键(attributeDict)的字典...当 elementName 等于“enclosure”时,查找键入“url”的条目...
Here is explanation: Example xml with attribute:这是解释:示例 xml 与属性:
Use xml delegate method:使用 xml 委托方法:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"remoteContent"]){
NSString* href = [attributeDict objectForKey:@"href"];
NSLog(@"href %@",href);
[someArray addObject:href];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.