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.
Actually not correct, since this will get "user/anynodelist/option",
The MS implementation is actually equivalent to selectNodes(".//name"), ie selectNodes(".//user/optio
For XPATH in FireFox look at https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript