Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

xml DOM difference between IE and Firefox

Posted on 2009-05-12
5
Medium Priority
?
1,390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 12

Assisted Solution

by:alien109
alien109 earned 248 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 252 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

704 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