【问题标题】:Parse HTML tag from RSS CDATA从 RSS CDATA 解析 HTML 标签
【发布时间】:2013-06-27 14:39:23
【问题描述】:

我想问是否可以从RSS中的CDATA标签中解析出标签?

我使用了一个基本的 jquery 函数从 RSS 中获取 JSON 对象,这是我的代码:

 $.get("someURL", function(data) {
            console.log('InGet');
            var $xml = $(data);
            $xml.find("item").each(function() {
                var $this = $(this),
                    item = {
                        title: $this.find("title").text(),
                        link: $this.find("link").text(),
                        description: $this.find("description").text(),                        
                        pubDate: $this.find("pubDate").text(),
                        author: $this.find("author").text()
                }
            });
        });

现在我想从描述中的 CDATA 获取 html 标签,但似乎 find() 对 item.description 不起作用。

这是一个简单的 RSS 结构

<item>
  <description>
      <![CDATA[<img .....> some content<div>...</div>]<br><br> some content]>
  </description>
</item>

那么我有什么方法可以解析 CDATA,就像我们对那些 xml 标签所做的那样?

【问题讨论】:

    标签: jquery html json xml-parsing rss


    【解决方案1】:

    只需使用 jQuery。

    description: $( $this.find("description").text() )
    

    【讨论】:

    • 那么这会返回一个普通的jquery对象吗?描述: $( $this.find("description").text() )。 find("some tag") 似乎也不适合我
    • 如果 &lt;description&gt; 包含 CDATA 则 $this.find("description").text() 将返回实际文本 - 即 HTML 字符串。您可以将其传递给 jQuery 来解析它。 console.log($this.find("description").text()) 输出什么?
    • 当然你需要使用$xml = $.parseXML(data) (docs)。我错过了那个。
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 2011-05-18
    • 2014-02-09
    • 2020-07-24
    • 2013-11-11
    • 2012-04-02
    • 2015-07-02
    • 1970-01-01
    相关资源
    最近更新 更多