ladders81
ladders81

Reputation: 51

Double XML DOM getAttribute() Method + condition

I have an XML file that I want to pull some attributes from using JavaScript, but I only want it to return the attributes that appear if the attribute from a parent node matches a global variable which I have set in a previous script as part of the HTML page.

My XML file is below

<?xml version="1.0" encoding="UTF-8"?>
<directory name="weeks">
<directory name="week42">
<file name="one.pdf"/>
<file name="two.pdf"/>
<file name="three.pdf"/>
</directory>
<directory name="week43">
<file name="four.pdf"/>
<file name="five.pdf"/>
</directory>
</directory>

The idea is to list the 'name' attribute of the file nodes but only if the 'name' attribute from the directory parent node = a global variable 'week' that is set in an earlier script. The week variable will change based on the date (XML file will update as weeks get added & deleted), but for this example we can presume it has been set as week42

My code so far is below. It writes all of the 'name' attributes from all file nodes so my result is

one.pdf
two.pdf
three.pdf
four.pdf
five.pdf

--

function loadXMLDoc(weekslinks)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","weeksdirlist.xml",false);
xhttp.send();
return xhttp.responseXML;
}  

xmlDoc=loadXMLDoc("weeksdirlist.xml");
x=xmlDoc.getElementsByTagName("file");

for (i=0;i<x.length;i++) 
{

var filename = x[i].getAttributeNode("name").nodeValue; //the nodefile is the filename
if (filename != "Thumbs.db")     
{
document.write(filename);
}
}

What I want is for my output to only return the 'name' attributes if the parent name attribute = my global variable which is week42. My output should therefore be

one.pdf
two.pdf
three.pdf

Any suggestions greatly appreciated. Thanks, Ladders

Upvotes: 0

Views: 307

Answers (1)

Naren
Naren

Reputation: 1477

Here we go....

    <!DOCTYPE html>
<html>
<body>
    <script>
        text = "<directory name='weeks'><directory name='week42'>";
        text = text + "<file name='one.pdf'/><file name='two.pdf'/>";
        text = text + "<file name='three.pdf'/></directory>";
        text = text + "<directory name='week43'><file name='four.pdf'/>";
        text = text + "<file name='five.pdf'/></directory></directory>";
        if (window.DOMParser) {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");
        } else // Internet Explorer
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(text);
        }
        var x = xmlDoc.getElementsByTagName('directory');

        var constant = 'week43';
        for (var i =0; i < x.length; i++) {
            var attrs = x[i].attributes;
            var k = attrs.getNamedItem("name").nodeValue;
            if (constant == k) {
                if (x[i].childNodes) {
                    document.write(k);
                    for ( var j = 0; j < x[i].childNodes.length; j++) {
                        var detail = x[i].childNodes[j];
                        var lattr = detail.attributes.getNamedItem("name").nodeValue;
                        document.write("<br>");
                        document.write(lattr);
                    }
                }
            }

        }


    </script>
</body>
</html>

ouput will be

week43 four.pdf five.pdf

Upvotes: 1

Related Questions