[AJAX/PHP/JS] Indexjumps in XML data, c'mon in and I'll explain!

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:

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?
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
I would just use JSON, as it can be natively parsed in JavaScript (though there are more safe libraries around than the native method).

It's very easy to create JSON in PHP as well.
 

UndeadDragon

Super Moderator
Reaction score
447
Which element is undefined? The parent one or one of the child elements?
 

GooS

Azrael
Reaction score
154
Lyerae, I think I'll do that, however I'm still curious as to why I have to jump 1 element.

UndeadDragon, the elements not used in the code are undefined, the root element exists [0], but then I have to jump every other child to get defined elements, and then every two children of that element.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Don't waste time trying to figure out why XML isn't working when you can use JSON instead.

Leave XML for other stuff that can't parse JSON.
 

GooS

Azrael
Reaction score
154
I have remade it into JSON and it works like a charm when implemented into the application I made it for!

But even so I am still curious as to why the XML problems occured!
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
I have remade it into JSON and it works like a charm when implemented into the application I made it for!

But even so I am still curious as to why the XML problems occured!

XML is the dinosaur version of JSON, don't even bother figuring it out.
Just be happy the web has moved on to JSON and smile when you see other application developers still having to struggle with XML :cool:
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
I'm curious about the problem as well...
(I'm not an expert parsing XML, but...)

Code:
for(var i = 1; i < (xmlDoc.getElementsByTagName('anchor').length*2); i += 2)

Shouldn't that be...

Code:
for(var i = 1; i < (xmlDoc.getElementsByTagName('anchor').length*2); i += 1)
?
 

JerseyFoo

1/g = g-1
Reaction score
40
Lyerae, read for detail.

This occurs because modern browsers consider white-space their own elements.

---
About your logic, there's no reason to have a "anchors" node.

Could have also avoided problems by doing it right.
PHP:
var anchors = xmlDoc.getElementsByTagName('anchor'), a;
var txt = '', endl = '<br/>';

while ( (a = anchors.shift()) ){
    txt += a.name +  endl + a.type + endl + a.href + endl + endl + endl;
}
Works with both regardless of XML, JSON, white-space or not. Is much faster, and a lot easier to read.
 

GooS

Azrael
Reaction score
154
Seems odd to consider whitespace an element, ah well, thanks for answering that one!

Really? I don't need a root in an XML structure? (Multiple anchor nodes are added below that's why I wrote ...)

That's some neat code you got there, just two whings I wonder wonder about.

Wouldn't the first assignment attempt to set anchors to a, which is yet to be defined in the code?

And guessing it's typos that you refer to a as e in the loop.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
By the way, irrelevant to the OP, but relevant to loops:

PHP:
for(var i = 1; i < (xmlDoc.getElementsByTagName('anchor').length*2); i += 1)
is a bad habit. If you use that type of loop on DOM collections (or even XML collections), you may get an endless loop.

It's much safer to do this instead:
PHP:
for(var i = 1,len = xmlDoc.getElementsByTagName('anchor').length*2; i < len; i ++)

Then it could be optimized to:
PHP:
for(var i = xmlDoc.getElementsByTagName('anchor').length*2; i--;)
Which is more than twice as fast, but reverts the order of your loop, so only use it when that doesn't matter.
 

JerseyFoo

1/g = g-1
Reaction score
40
Code:
var anchors = xmlDoc.getElementsByTagName('anchor'), a;
var txt = '', endl = '<br/>';
...is the same as...
Code:
var anchors = xmlDoc.getElementsByTagName('anchor');
var a;
var txt = '';
var endl = '<br/>';
Just a way of organization.

Yes that was a typo, I usually use 'e' as a local var for elements.

Really? I don't need a root in an XML structure?
Nope.

To elaborate on what Magnetix said...
PHP:
var e;
for ( var i = 0;  ( e = array.shift() ); i++ ){
    // this would also work, setting both i and e (the key and element)
    // this is because FOR actually performs a conditional on the middle statement, as it is performing a conditional on ( i-- ) above.
}

PHP:
var i = 1;
if ( i-- )
    document.write(i);  // OUTPUTS 0

i = 1;
if ( --i )
    document.write(i);  // doesn't run

It would've helped a lot if I knew these little things off the bat, maybe you'll find it useful someday.

----

To avoid the white-space problem, as you will run into this, you're actually supposed to use a new set of properties designed for Elements only. https://developer.mozilla.org/en/DOM/element.children
Code:
parent.children[]
parent.firstElementChild
parent.lastElementChild
parent.firstElementChild.nextElementSibling
Problem is, only the newest browsers support this, even though it's been a production standard for nearly 2 years now. I personally believe the entire white-space node stuff is ridiculous and they should've used entities for it, but some higher power supports this. At least now if you see someone claiming it's pointless to strip whitespace from your PHP/etc output, you know they don't have their details straight.
 

GooS

Azrael
Reaction score
154
Magentix, the first thing you said, guessing you're talking about live objects, they could be inserted into and therefor increase in length while looping making it infinite, don't see a problem there mainly because I don't append anything into that element anywhere in my code.

Yes hat loop could be optimized, which it has been, getting a property of an array of a document every itteration is pretty heavy just to get the length number.
I now use a do while with the same arguments that are in your last loop there, don't know which is faster if there are any diffrences, but I like the look of it!

Thanks for the comment!

JerseyFoo, I must read closer into the comma operator and thanks for the loop and whitespace as elements info!
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top