我已经看到了处理此问题的帖子,但我仍然无法解决我的问题:
我有带有 CDATA 的 XML,当我解析 XML 时,它包含 CDATA(我不想要)。
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?
}
});
}
看起来导航标签中有两个名为 title 的子项。当您执行以下操作时,您将同时返回:
$(this).find("title").text()
尝试使用:
$(this).find("title:first").text()
另外,删除条件:
dataType: ($.browser.msie) ? "xml" : "text/xml",
只需使用:
dataType: "xml",
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)