0

I need a way to check if a XML node value is empty or null because my code fails if any node comes up empty.

<!-- 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;

If any of the above vars look at an empty node in my xml the code breaks.

Sample XML code:

<?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>

The xml is regenerated every time a song ends and sometimes a node is not supplied with a value

11
  • use jQuery to traverse the xml content Commented Jan 7, 2014 at 0:10
  • like var $xml = $(xml); var stationid = $xml.find('stationid').eq(0).children(':eq(0)').text() Commented Jan 7, 2014 at 0:11
  • can you share the xml sample as well Commented Jan 7, 2014 at 0:16
  • Sure here is a sample xml in edit above Commented Jan 7, 2014 at 0:31
  • then it should as simple as var $xml = $(xml); var stationid = $xml.find('stationid').text(); can you try this Commented Jan 7, 2014 at 0:36

1 Answer 1

1

I would prefer to use jQuery traversal to find the values

var $xml = $(xml); 
var stationid = $xml.find('stationid').text();

As I said in the comments it might be litter slower than native dom traversal, but it shouldn't be a problem unless dealing with 100s of records. It increases the readability of the code a lot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.