xcode iOS:NSXMLParser 获取属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8357040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:39:19  来源:igfitidea点击:

iOS: NSXMLParser get attribute

xcodeios5nsxmlparser

提问by tbleckert

I'm really new to this and I'm trying to read an RSS feed, and so far everything is good. This is how I do it:

我真的很陌生,我正在尝试阅读 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);
  }
}

The problem is the enclosure. Which looks like this in the XML:

问题是外壳。在 XML 中看起来像这样:

<enclosure length="150" url="urltoimage.jpg" type="image/jpeg" />

How do I get urlattribute from that element from within my function?

如何从我的函数中从该元素获取url属性?

EDITThe XML looks like this:

编辑XML 如下所示:

<item>
    <title>...</title>
    <link>...</link>
    <description>...</description>
    <pubDate>...</pubDate>
    <enclosure length="150" url="urltoimage.jpg" type="image/jpeg" />
</item>

And my foundCharacters function looks like this:

我的 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];
  }
}

回答by Tendulkar

- (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
}

回答by Denis

You have to handle one more callback for tracking attributes:

您必须再处理一个用于跟踪属性的回调:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

attributeDictis dictionary with all found element arguments

attributeDict是包含所有找到的元素参数的字典