Link to home
Start Free TrialLog in
Avatar of Melvinivitch
Melvinivitch

asked on

IE and Firefox node tree indexing differences

I'm new to XML. I've found that I get different values in IE6 vs FireFox2.0 (and FF1.5) for identical traversals of my simple xml node tree.

Here's my XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<store>
      <item iName="widget1">
            <title> Widget One </title>
            <baseprice> 35.00 </baseprice>
      </item>

      <item iName="widget2">
            <atitle> Widget Two </atitle>
            <abaseprice> 1500.00 </abaseprice>
            <repeat> x1 </repeat>
            <repeat> x2 </repeat>
            <repeat> x3 </repeat>
            <terminal> end </terminal>
      </item>

      <item iName="widget3">
            <ptitle> widget3 </ptitle>
            <pbaseprice> 8500.00 </pbaseprice>
            <repeat> y1 </repeat>
            <repeat> y2 </repeat>
            <repeat> y3 </repeat>
            <terminal> endw3 </terminal>
      </item>

</store>


And here's the relevant js code:

      var allItems = xmlDoc.getElementsByTagName("item");
      var iTitle = allItems[1].childNodes[0].nodeName;
      var iPrice = allItems[1].childNodes[1].nodeName;

The results:

In IE, iTitle = "atitle" and iPrice = "abaseprice", as expected.
In FF, iTitle = "#text" and iPrice = "atitle".


So it's behaving like it should (at least in my mind) in IE, but not in FireFox.

In any event, why do they give me different values?? Do developers have to come up with browser-specific xml node-tree code?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Melvinivitch
Melvinivitch

ASKER

Bloody hell.
I feared something like that. Actually, I feared (though felt silly as it's so absurd) EXACTLY that. I did recall from the w3schools tutorial on xml that the model treated whitespace as relevant, but I didn't bother completely testing that as I only half-heartedly even considered it as a possible culprit because rigorously treating whitespace such as newlines and tabs as data AND some browsers doing it differently than others would be incredibly obnoxious. Furthermore, if XML is supposed to elegantly represent data, wouldn't it be beneficial for it to be as human-readable as possible?? BAH! I digress.

Thanks very much for the info and suggestions. I'm sure (unfortunately) the work-around ideas will come in handy.


This is tremendously unpleasant. I should go back to strictly being a non-web programmer...



In case anyone's still listening...

Can I avoid these browser issues by using PHP5's SimpleXML to parse my XML file on the server-side, and not have to worry about the browsers' parser differences at all?
If you run into browser incompatibilities,
and you can solve stuff on the server,
you should do the works serverbased
so yes, go for PHPs XML support and work from the server

If you really want to stick to browser operations
you can try to stuff a layer inbetween
here is a good example of such an intermediate library
http://mochikit.com/about.html

cheers