0
我需要一種方法來檢查XML節點值是否爲空或空,因爲如果任何節點出現空白,我的代碼將失敗。空的XML節點導致錯誤使用jQuery .ajax
<!-- Get Playlist XML from radio_info dir -->
function getPlaylist() {
$.ajax({
type: "GET",
url: radioPath, // ** Set path to radio_info XML here **
cache: false,
dataType: "xml",
error: function(jqXHR, textStatus, errorThrown) {
alert("error:" + errorThrown);
},
success: nowPlaying
//nowPlaying found the XML and retreived it
});
};
//nowPlaying gets vars from xml on getPlaylist success
var stationid = xml.getElementsByTagName("stationid")[0].childNodes[0].nodeValue;
var currentdate = xml.getElementsByTagName("currentdate"[0].childNodes[0].nodeValue;
var artist = xml.getElementsByTagName("artist")[0].childNodes[0].nodeValue;
var title = xml.getElementsByTagName("title")[0].childNodes[0].nodeValue;
var album = xml.getElementsByTagName("album")[0].childNodes[0].nodeValue;
var year = xml.getElementsByTagName("year")[0].childNodes[0].nodeValue;
var pStart= xml.getElementsByTagName("timestamp")[0].childNodes[0].nodeValue;
var duration = xml.getElementsByTagName("duration")[0].childNodes[0].nodeValue;
var type = xml.getElementsByTagName("type")[0].childNodes[0].nodeValue;
var artfile = xml.getElementsByTagName("artfile")[0].childNodes[0].nodeValue;
var currentfile = xml.getElementsByTagName("currentfile")[0].childNodes[0].nodeValue;
var recent1 = xml.getElementsByTagName("recent1")[0].childNodes[0].nodeValue;
var recent2 = xml.getElementsByTagName("recent2")[0].childNodes[0].nodeValue;
var recent3 = xml.getElementsByTagName("recent3")[0].childNodes[0].nodeValue;
var recent4 = xml.getElementsByTagName("recent4")[0].childNodes[0].nodeValue;
如果任何上述變量在我的xml中查看一個空節點,代碼會中斷。
示例XML代碼:
<?xml version="1.0" encoding="ISO-8859-1"?>
<event>
<station>
<stationid><![CDATA[MIKE'S FAMOUS RADIO]]></stationid>
<currenttime><![CDATA[7:30 pm]]></currenttime>
<currentdate><![CDATA[January 06, 2014]]></currentdate>
</station>
<song>
<artist><![CDATA[Spirit]]></artist>
<title><![CDATA[I Got a Line on You]]></title>
<album><![CDATA[The Family That Plays Together]]></album>
<timestamp>7:30 pm</timestamp>
<duration>02:38</duration>
<type><![CDATA[MUSIC]]></type>
<url><![CDATA[]]></url>
<year>1968</year>
<genre><![CDATA[Rock]]></genre>
<cdfile><![CDATA[505-13]]></cdfile>
<copyright><![CDATA[]]></copyright>
<composer><![CDATA[California]]></composer>
<publisher><![CDATA[Hollenbeck]]></publisher>
<comments><![CDATA[]]></comments>
<artfile><![CDATA[The Family That Plays Together.jpg]]></artfile>
<currentfile><![CDATA[505-13]]></currentfile>
</song>
<recent>
<recent1><![CDATA[7:26 pm - Talking Heads - Take Me to the River (1978)]]></recent1>
<recent2><![CDATA[<!--BSIRECENTX2-->]]></recent2>
<recent3><![CDATA[<!--BSIRECENTX3-->]]></recent3>
<recent4><![CDATA[<!--BSIRECENTX4-->]]></recent4>
</recent>
</event>
XML是再生每一個歌曲結束,有時一個節點沒有價值,提供的時間
使用jQuery遍歷xml內容 –
like'var $ xml = $(xml); var stationid = $ xml.find('stationid')。eq(0).children(':eq(0)')。text()' –
你能不能分享xml樣本 –