GooS
Azrael
- Reaction score
- 154
So the title may not help much so here's the deal.
I wish to get a directory listing into javascript, using AJAX and a PHP script.
The PHP script alters the header so that the response is an XML document and it presents the data generated as XML.
Such as:
[Generated XML document]
(The items listed are just random, popped some in there for testing purposes)
Ok, so far so fine, I created the AJAX, also worked, so lets jump to now.
The script works, if you can call it that, BUT, in my loop through the XML
data I must add 2 indexes per itteration as every other element is 'undefined'
which I find quite odd. I must also double up the max index (list size) in order
to get all elements, it solves the problem and gives me no new ones cept for
the fact that it all seems very strange. Here's the test response:
(Yes the child elements must skip one aswell, there are three for every anchor node but 0,2,4 are undefined)
(index must start at 1 not to try and fetch the first element which is undefined)
tldr; Odd problem, every other element in my XML object is undefined, what's up?
I wish to get a directory listing into javascript, using AJAX and a PHP script.
The PHP script alters the header so that the response is an XML document and it presents the data generated as XML.
Such as:
Code:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<anchors>
<anchor>
<name>subdir</name>
<type>dir</type>
<href>root/subdir</href>
</anchor>
...
</anchors>
[Generated XML document]
(The items listed are just random, popped some in there for testing purposes)
Ok, so far so fine, I created the AJAX, also worked, so lets jump to now.
The script works, if you can call it that, BUT, in my loop through the XML
data I must add 2 indexes per itteration as every other element is 'undefined'
which I find quite odd. I must also double up the max index (list size) in order
to get all elements, it solves the problem and gives me no new ones cept for
the fact that it all seems very strange. Here's the test response:
(Yes the child elements must skip one aswell, there are three for every anchor node but 0,2,4 are undefined)
(index must start at 1 not to try and fetch the first element which is undefined)
Code:
var xmlDoc = xmlhttp.responseXML;
txt = "";
for(var i = 1; i < (xmlDoc.getElementsByTagName('anchor').length*2); i += 2){
name = xmlDoc.childNodes[0].childNodes[i].childNodes[1].childNodes[0].nodeValue + '<br />';
type = xmlDoc.childNodes[0].childNodes[i].childNodes[3].childNodes[0].nodeValue + '<br />';
href = xmlDoc.childNodes[0].childNodes[i].childNodes[5].childNodes[0].nodeValue + '<br />';
txt += name + type + href + '<br /><br />';
}
document.getElementById('result').innerHTML = txt;
tldr; Odd problem, every other element in my XML object is undefined, what's up?