jQuery和XML(带CDATA) [英] jQuery and XML (with CDATA)

查看:46
本文介绍了jQuery和XML(带CDATA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过处理此问题的帖子,但我仍然无法解决我的问题:

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

我用CDATA获取XML并在解析时XML,它包括CDATA(我不想要)。

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

XML样本:


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

}

推荐答案

看起来nav标签中有两个名为title的子节点。当你这样做时,你会回来:

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

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

尝试使用:

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

另外,删除条件:

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

只需使用:

dataType: "xml",

这篇关于jQuery和XML(带CDATA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆