Solved

IE and Firefox node tree indexing differences

Posted on 2006-11-16
4
358 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:Melvinivitch
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 400 total points
ID: 17954989
Hi Melvinivitch,

Let me start by disappointing you...
You can not blindly rely on IE and FF to do the same thing.
They don't share the same methods.
They behave differently using methods they do share
...
It is very though to create a DOM driven cross browser XML based web page.
I very often revert to XSLT if I want it to work in both browsers.

One of the issues you meet here, is the undefinedness of the XML data model ( in  a sense)
msxml (the microsoft activeX component used for dealing with XML in a browser) has a weird way of dealing with white-space only text nodes.
If an element has only child elements and white-space only text elements, the white-space nodes are considered cosmetical (they usually are)
According to the XML recommendation these white-space nodes are relevant.
So I hate to say, but IE is pragmatic, FF is correct

childNodes[0] is the nameless text node for FF (and that is correct)
childNodes[0] is the ptitle node for IE, because it ignores the whitespace

If this were your XML
<item iName="widget3"><ptitle> widget3 </ptitle><pbaseprice> 8500.00 </pbaseprice>
both parsers would respond equally

There are different ways to work around this issue:
- you could explicitely ask for a specific childnode
    something like var iTitle = allItems[1].getElementsByTagName("ptitle"); should work
- you could add a test and pick the first childnode that is not a text node

hope this helps

Cheers!

Geert
0
 

Author Comment

by:Melvinivitch
ID: 17955078
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...



0
 

Author Comment

by:Melvinivitch
ID: 17993294
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?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17994081
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SVG, D3, scaling, translation 11 141
surrogate key in database world 6 83
Getting attribute Values using xslt 4 41
How to use xmlReader and idatareader  ? 4 55
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now