简体   繁体   English

如何获取自定义元素属性而不是Feedzirra中的值

[英]How to get custom element attributes not values in Feedzirra

I have an RSS feed that looks like 我有一个RSS供稿,看起来像

<item>
<title>Blah</title>
<enc:enclosure resource="http://hello.jpg" type="image/jpeg"/>
</item>

Using Feedzirra I am trying to retrieve the URL of the resource. 我正在使用Feedzirra尝试检索资源的URL。

I have tried 我努力了

Feedzirra::Feed.add_common_feed_entry_element('enc:enclosure', :as => :img)
feed.entries.first.img

And that always returns Nil, which I think is because the element has no value. 而且总是返回Nil,我认为这是因为元素没有价值。
I also tried 我也试过

 Feedzirra::Feed.add_common_feed_entry_element('enc:enclosure', :resource => :res, :as => :img)
 feed.entries.first.res

And img is nil and res returns undefined method. 并且img为nil,res返回未定义的方法。

So how to I get the elements attribute value "resource"? 那么,如何获取elements属性值“ resource”?

After really looking into the rss_entry.rb 在真正研究了rss_entry.rb之后

https://github.com/feedjira/feedjira/blob/master/lib/feedjira/parser/rss_entry.rb https://github.com/feedjira/feedjira/blob/master/lib/feedjira/parser/rss_entry.rb

I figured out how I had it backwards by trying to follow the "media:content" example 通过尝试遵循“ media:content”示例,我弄清楚了它是如何倒退的

So here is the correct code. 所以这是正确的代码。

Feedzirra::Feed.add_common_feed_entry_element('enc:enclosure', :as => :img, :value=>:resource)
feed.entries.first.img

returns " http://hello.jpg " 返回“ http://hello.jpg

If I understand it right, the :as tells the parser what I want to call it. 如果我理解正确,则:as告诉解析器我要调用它什么。 And then sets the value of that to the attribute of interest, in this case resource. 然后将其值设置为感兴趣的属性,在本例中为资源。

Hope this helps someone. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM