【问题标题】:Jquery: parse tag inside CDATAJquery:解析CDATA内的标签
【发布时间】:2014-02-09 14:35:43
【问题描述】:

我正在尝试解析嵌套在 CDATA 值中的标记:

基本上,我有以下xml:

<item>
            <title>Time Travel Via Wormhole Breaks the Rules of Quantum Mechanics</title>
            <description><![CDATA[<p>Science has done it again everybody! Brace yourselves for this groundbreaking news, freshly determined by physicists: Time travel, if it exists, may have some weird consequences. Gosh, who’d have thunk it? But no, seriously, a recent article suggests that a certain kind of theoretically possible time machine would wreak minor havoc with a firm principle [&#8230;]</p><p>The post <a href="http://blogs.discovermagazine.com/crux/2014/01/16/time-travel-via-wormhole-breaks-the-rules-of-quantum-mechanics/">Time Travel Via Wormhole Breaks the Rules of Quantum Mechanics</a> appeared first on <a href="http://blogs.discovermagazine.com/crux">The Crux</a>.</p>]]></description>
            <content:encoded><![CDATA[<p><a href="https://i.stack.imgur.com/kUmJM.jpg"><img class="aligncenter  wp-image-3898" alt="time-travel" src="https://i.stack.imgur.com/kUmJM.jpg" width="600" height="405" /></a></p>
<p>Science has done it again everybody! Brace yourselves for this groundbreaking news, freshly determined by physicists: Time travel, if it exists, may have some weird consequences. Gosh, who’d have thunk it?</p>
<p>As with all speculative science stories, it’s important to keep things in perspective. This finding would have far-reaching and serious consequences for Internet encryption and quantum computers, among other things — assuming these wormholes really do exist. But, equally valid, the fact that this theoretical construction appears to violate known physical laws also suggests that, alas, maybe the particular wormholes in the study just don’t exist.</p>
<p>Whatever tricks the universe has up its sleeve, it’s exciting that we’re able to study even its wackiest possibilities in so much detail. I can&#8217;t wait to see how it turns out (no spoilers, time travelers).</p>
<p><em>Image courtesy <a id="portfolio_link" href="http://www.shutterstock.com/gallery-73592p1.html">Graeme Dawes </a>/ <a id="portfolio_link" href="http://www.shutterstock.com/gallery-551845p1.html">Ilias Strachinis </a>/ Shutterstock</em></p>
<p>The post <a href="http://blogs.discovermagazine.com/crux/2014/01/16/time-travel-via-wormhole-breaks-the-rules-of-quantum-mechanics/">Time Travel Via Wormhole Breaks the Rules of Quantum Mechanics</a> appeared first on <a href="http://blogs.discovermagazine.com/crux">The Crux</a>.</p>]]></content:encoded>
</item>

我可以正确解析标题、描述和内容:编码标签,所有 CDATA 值如下:

$(this.data).find('item:lt(3)').each(function(index) {
            var e = $(this);
            console.log(e);
            var category    = e.find('category').text();
            var link        = e.find('link').text();
            var title       = e.find('title').text();
            var summary     = e.find('description').text().substring( 0, 120 ) + "...";
            var content     = e.find('encoded').text();
            var image       HOW TO EXTRACT
            alert(image);

我缺少的是图片的 URL,不幸的是,它不像其他一些 RSS 提要到特定元素中那样: 例如:

<enclosure type="image/jpeg" url="http://www.nwzonline.de/rw/NWZ_CMS/NWZ/2011-2013/Produktion/2014/01/17/SPORT/2/Bilder/generated/SPORT_1_8d5e0b63-8d51-4e87-8249-58eab44cc923--600x337--280x158.jpg"></enclosure>
var image = e.find('img').attr('url');

但是在 CDATA 里面。知道如何从中提取 src 值吗?我需要获取:“http://blogs.discovermagazine.com/crux/files/2014/01/time-travel.jpg

非常感谢。

【问题讨论】:

  • 你不能像处理this.data一样处理content吗?

标签: javascript jquery xml rss


【解决方案1】:

CDATA 中没有标签。 CDATA的意思是“这里的东西包含看起来像标签的东西,但它们不是标签,它们是普通的字符数据”。这就是 CDATA 的唯一目的,就是说里面没有标签;如果您希望将标签视为标签,请不要将它们错误标记为 CDATA。

如果其他人犯了这个错误而你必须纠正它,那么唯一的方法是提取 CDATA 标记内的字符串并将其传递给 XML 解析器以解析成树。

【讨论】:

  • 输入来自这个 rss 提要视图源:feeds.feedburner.com/Discovercrux 我最后做了子串,但我认为这样做真的很糟糕......跨度>
  • @user280560 是的,这不是一个好方法,这就是为什么建议您使用 XML 解析器处理 content
  • 是的,但是 CDDATA 内容以

    标记和另一个

    标记元素开头,我认为不是一个有效的 XML 来解析吗?

  • CDATA当然可以包含字符串"&lt;p&gt;",但是当该字符串出现在CDATA中时就不是标签了。
猜你喜欢
  • 2013-06-27
  • 2015-07-02
  • 2013-11-25
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
相关资源
最近更新 更多