6

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:

<mainnav>
    <nav path="/" xmlpath="home.xml" key="footer" navigator="">
        <display><![CDATA[Home]]></display>
        <title><![CDATA[Home]]></title>
    </nav>

    <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>
        </sub>

    </nav>


    <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>
        </sub>

    </nav>

</mainnav>

jQuery:

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

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

}

Pico
  • 331
  • 1
  • 2
  • 14

3 Answers3

11

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

$(this).find("title").text()

Try using:

$(this).find("title:first").text()

Also, remove the conditional:

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

And just use:

dataType: "xml",
Sandro
  • 4,761
  • 1
  • 34
  • 41
  • oh thanks. the dataType did the trick. d'oh! but IE6 won't display anything now. any ideas? – Pico Apr 02 '10 at 19:16
  • Are you testing this locally? IE6 doesn't seem to like that. Probably because the correct headers aren't being sent as they are when they are on a web server. Try putting the XML file on a webserver or use this to test: http://digitalpadin.com/test.xml Source: http://groups.google.com/group/jquery-en/browse_thread/thread/adb2b047f3761179?pli=1 – Sandro Apr 02 '10 at 19:48
  • it's all running on a web server. – Pico Apr 02 '10 at 20:30
1
<PRODUCTS>
  <COD>1</COD>
  <NAME><![CDATA[MINISYSTEM SONY VAIO]]></NAME>
</PRODUCTS>


        function CDATA(str){            
            var res = str.substr(9,str.length-12)
            return res
        }

        CDATA($(this).find("name").text());
0

OK found the missing piece on another forum:

<script type="text/javascript"> instead of: <script type="application/javascript">

madhead
  • 31,729
  • 16
  • 153
  • 201
Pico
  • 331
  • 1
  • 2
  • 14