Solved

xml DOM difference between IE and Firefox

Posted on 2009-05-12
5
1,375 Views
Last Modified: 2013-12-07
Say I have an XML string that looks something like this:

<users>
   <user>
     <option value="foo">bar</option>
     <option value="foo2">bar2</option>
   </user>
   <user2>
     <option value="foo">bar</option>
     <option value="foo2">bar2</option>
  </user2>
</users>

I can load this into an XML doc on IE like this:

    txt = ... // xml string
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(txt);

On Firefox, like this:

    var parser = new DOMParser();
    xmlDoc = parser.parseFromString(txt, "application/xml");

So far, everything is good.

When I want to extract parts of the document, on IE I can do this:

xmlObjs = xmlDoc.getElementsByTagName("user/option");

This gives me a collection of the two options under user.

However, it does not work on Firefox - I get nothing.

I can do this:

xmlObjs = xmlDoc.getElementsByTagName("option");

but this gives me 4 nodes - two under user, and two under user2.

I could do this:

xmlObjs = xmlDoc.getElementsByTagName("user");

and walk the children looking for those nodes with a value of "option", but this takes a lot more code.

How do I do

xmlObjs = xmlDoc.getElementsByTagName("user/option");

in Firefox? I am converting a large web app to be able to run on both IE and Firefox, and the above line is in many places, I'm trying to minimize the changes I have to make.

Thanks.



0
Comment
Question by:wayside
  • 2
  • 2
5 Comments
 
LVL 12

Assisted Solution

by:alien109
alien109 earned 62 total points
ID: 24369769
I believe the getElementsByTagName is supposed to only work on a single tag name, and that IE is actually doing something it technically shouldn't.

To get around this, you can first grab the user node, and then grab it's child nodes as follows:

var optionNodes = xmlDoc.getElementsByTagName("user")[0].getElementsByTagName("option");

Hope that helps.
0
 
LVL 27

Accepted Solution

by:
BigRat earned 63 total points
ID: 24373564
>>var optionNodes = xmlDoc.getElementsByTagName("user")[0].getElementsByTagName("option");

Actually not correct, since this will get "user/anynodelist/option", but that's a minor worry.

The MS implementation is actually equivalent to selectNodes(".//name"), ie selectNodes(".//user/option'), it does not bother to check the parameter before using selectNodes(). Unfortunately the Mozilla people have gone out of their way to produce an XPath implementation on the DOM (W3C has NOT defined an XPATH interface to the DOM), which is incompatible with Microsoft (who incidentally were first with their XPATH implementation). So you'll just have to select the user elements and then test each child element for an "option" element.

For XPATH in FireFox look at https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript
0
 
LVL 27

Expert Comment

by:BigRat
ID: 24373672
PS: You might try using sarissa (http://dev.abiss.gr/sarissa/) which does tend to hide a lot of differences and allows you to use XPATH expressions. In fact the getElementsByTagName does have the performance detriment of scanning the entire document, the direct approach /result/user/options, for example, would give you a node list overwich you could iterate.
0
 
LVL 12

Expert Comment

by:alien109
ID: 24373681
@BigRat - that is correct, and thanks for the extra info. Helpful to me too!

wayside, another option if you can, is to use jQuery (www.jquery.com). Not only will it simplify your loading of the XML, but it gives you a lot more options for traversing and selecting nodes in your XML object.

For example, with jQuery, you can do the following - which will give you exactly what you want:

var options = $("user>option");

Hope that helps.
0
 
LVL 14

Author Comment

by:wayside
ID: 24398583
Thanks for the responses, they've been helpful.

Unfortunately using anything open-source opens up a huge can of worms, so it is not really an option.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 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

10 Experts available now in Live!

Get 1:1 Help Now