Filtering XML nodelist with Ajax and JavaScript
17:42 28 Mar 2017

I'm trying to build a page with AJAX and JavaScript that displays the contents of an XML file filtering it by node value. The XML is similar to this:



  
    Empire Burlesque
    Bob Dylan
    USA
  
  
    Hide your heart
    Bonnie Tyler
    UK
  
  
    Greatest Hits
    Dolly Parton
    USA
  
  
    Still got the blues
    Gary Moore
    UK
  

After the XMLHttpRequest, the CD data are converted to an array (nodeList) before being displayed in a table:

function handleXML (xmlData) {
    var data = xmlData;
    var cd = data.getElementsByTagName("cd");
    var arrayCD = Array.prototype.slice.call(cd);

     var table = "";
     table += "";

     for ( var i = 0, len = arrayCD.length; i < len; i++ ) {
            table += "";
            table += "";
            table += "";
            table += "";
            table += "";
        }

        table += "";
        table += "
"+ arrayCD[i].getElementsByTagName("title")[0].firstChild.nodeValue +""+ arrayCD[i].getElementsByTagName("artist")[0].firstChild.nodeValue +""+ arrayCD[i].getElementsByTagName("country")[0].firstChild.nodeValue +"
"; return table; }

My question is how to filter the table data so that (for instance) only CDs produced in the UK are displayed. I've been struggling with this for a while and now begin to feel more and more confused. I suppose a solution could be something like:

 arrayCD.filter(function(c) {
        var filterC = c.getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue;
        return (filterC = "UK");
    });

(which should be inserted between var arrayCD and var table). But I simply can't make it work. I'd be glad if somebody could suggest an answer.

javascript ajax xml nodelist