parse html inside cdata using jquery or javascript

Bahamut picture Bahamut · Feb 19, 2013 · Viewed 17.4k times · Source

I'm getting a website feed that looks like this

<rss...>
    <title> some title </title>
    <content>
         <![CDATA[ <div>this tag is ignored<div> who took the cookie in the cookie jar!?  ]]>
    </content>
</rss>

I need the entire content of the cdata to be displayed in the html. I'm using jquery 1.9.1 and when I get the content part using $(xml).find('rss content').text(), it actually ignores the whole <div>this tag is ignored<div> part. Any way to get everything inside the CDATA using javascript or jquery?

Answer

Fr&#233;d&#233;ric Hamidi picture Frédéric Hamidi · Feb 19, 2013

Chances are your markup is not parsed as XML by jQuery. Try explicitly invoking $.parseXML():

var contentText = $($.parseXML(xml)).find("rss content").text();