Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse html inside cdata using jquery or javascript

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?

like image 449
Bahamut Avatar asked Feb 19 '13 15:02

Bahamut


People also ask

Can you use CDATA in HTML?

Note that CDATA sections should not be used within HTML; they only work in XML.

What is Javascript CDATA?

CDATA stands for Character Data and it tells the parser to copy the following code exactly (and not try to parse it.)

What does CDATA mean in HTML?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

Is CDATA deprecated?

Note: CDATA is now deprecated. Do not use. The CDATA Section interface is used within XML for including extended portions of text. This text is unescaped text, like < and & symbols.


1 Answers

bottomline:

xmlDoc.getElementsByTagName("content")[0].childNodes[0].nodeValue

this snippet from working code uses jquery to load xml and then gets the 4th occurence of the content tag (which contains CDATA)

var req = new AjaxRequest(); 
req.setMethod("POST"); 
...
req.loadXMLDoc(linkString, paramString);
var htmlContent = req.getResponse().responseXML.getElementsByTagName('content').item(3).childNodes[0].nodeValue;
like image 113
tony gil Avatar answered Sep 19 '22 15:09

tony gil