Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1393
  • Last Modified:

xml DOM difference between IE and Firefox

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
wayside
Asked:
wayside
  • 2
  • 2
2 Solutions
 
alien109Commented:
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
 
BigRatCommented:
>>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
 
BigRatCommented:
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
 
alien109Commented:
@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
 
waysideAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now