Solved

xml DOM difference between IE and Firefox

Posted on 2009-05-12
5
1,381 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
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…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

777 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