Skip to content Skip to sidebar Skip to footer

Extract Cdata From Rss Xml Using Javascript

I have extracted RSS feed content using JS, however the 'Description' node contains CDATA and I want to split this out. For example, for each Description node under Item I would li

Solution 1:

CDATA section content is just text, so you can't parse its contents further using the DOM. You can either use DOMParser() to reconstruct the string contents of the CDATA section back into XML and use DOM methods from there, or else use regular expressions.

To use the latter approach, change your document.write() line to this:

// Slice off 5 characters to get rid of the parent <div> and use [\s\S] to mean//   any character including newlines up until the first closing div tagdocument.write('<p>' + description[b].childNodes[0].nodeValue.slice(5).match(/[\s\S]*?<\/div>/) + '</p>');

To use the former approach, which is less than ideal in this case but could be helpful in other situations, you could do this inside the for loop:

var cdataContent = newDOMParser().parseFromString('<div xmlns="http://www.w3.org/1999/xhtml">'+description[b].childNodes[0].nodeValue+'</div>', 'text/xml').documentElement;
document.body.appendChild(cdataContent.firstChild);

...but being sure to only invoke media() after the DOM content has loaded.

And maybe you have some good reason for it, but based on the code you supplied, it'd be a lot simpler just to do this:

for (i=1; i<description.length; i++) {

...and forget about a and b (i.e., change b to i)

And one tip: if you construct the RSS yourself, note that you won't be able to use CDATA sections nested within CDATA sections.

Post a Comment for "Extract Cdata From Rss Xml Using Javascript"