Pico Pico - 2 years ago 191
Javascript Question

jQuery and XML (with CDATA)

I've seen the post that deal with this issue but I still can't solve my issue:

I've got XML with CDATA and when I parse the XML, it includes the CDATA (which I don't want).

XML sample:

<nav path="/" xmlpath="home.xml" key="footer" navigator="">

<nav path="/nav1/" xmlpath="nav1.xml" key="primary" navigator="primary" iconid="0">
<display><![CDATA[Nav 1]]></display>
<title><![CDATA[Nav 1]]></title>
<overdesc><![CDATA[test nav 1]]></overdesc>

<sub path="/nav1/sub1/" xmlpath="nav1/sub1.xml" key="sub">
<display><![CDATA[sub 1<br />of nav 1]]></display>
<title><![CDATA[sub 1<br />of nav 1]]></title>


<nav path="/nav1/" xmlpath="nav2.xml" key="primary" navigator="primary" iconid="1">
<display><![CDATA[Nav 2]]></display>
<title><![CDATA[Nav 2]]></title>
<overdesc><![CDATA[test nav 2]]></overdesc>

<sub path="/nav2/sub1/" xmlpath="nabv2/sub1.xml" key="sub">
<display><![CDATA[sub 1<br />of nav 2]]></display>
<title><![CDATA[sub 1<br />of nav2]]></title>




type: "GET",
url: "site_xml/config.xml",
//contentType: "text/xml",
dataType: ($.browser.msie) ? "xml" : "text/xml",
success: parseXML,
error: function(XMLHttpRequest, textStatus, errorThrown) {

function parseXML(xml) {
if ($(this).attr("key")=="primary") { // this is a primary nav item;
var title = $.trim( $(this).find('title').text() );
$("#output").append(title); //nothing showing up in my output DIV, presumably due to the CDATA tags?


Answer Source

Looks like there are two children named title within the nav tag. You are getting back both when you do:


Try using:


Also, remove the conditional:

dataType: ($.browser.msie) ? "xml" : "text/xml",

And just use:

dataType: "xml",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download