jQuery and XML (with CDATA)

Posted by P.. on Stack Overflow See other posts from Stack Overflow or by P..
Published on 2010-04-02T18:36:09Z Indexed on 2010/04/02 19:03 UTC
Read the original article Hit count: 525

Filed under:
|
|
|

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?
     }
}); 

}

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about Xml