By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,371 Members | 2,863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,371 IT Pros & Developers. It's quick & easy.

Mozilla Vs IE for childNodes.length

P: n/a

HI

The Value for childNodes.length differs with mozilla and IE

Is it problem with my coding..... I could not under
stood.............
The following is the details

XML file

<track_order>
<status>0</status>
</track_order>

xmlDoc is the above XML

var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);

For Mozilla I get n.childNodes.length = 3

For IE I get n.childNodes.length = 1
Can any one please help

regards
moses

Oct 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Moses wrote:
The Value for childNodes.length differs with mozilla and IE
<track_order>
<status>0</status>
</track_order>

xmlDoc is the above XML

var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);

For Mozilla I get n.childNodes.length = 3

For IE I get n.childNodes.length = 1
Mozilla for XML and HTML document includes whitespace between elements
as text nodes in the DOM, IE does not do that for HTML at all and for
XML does not do it by default (although you can set preserveWhiteSpace =
true on an MSXML DOMDocument).
So for Mozilla that track_order element has as its first child node a
text node with white space, then the status element as the second child
and then again a text node with white space as the third child.
MSXML (used by IE) only has the element node.

That should not pose a problem at all, if you are looking for child
elements then ignore the childNodes collection and simply use
getElementsByTagName (e.g.
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
gives you the element) and/or XPath to access the child elements. If you
still think you need to loop through childNodes then include checks for
nodeType being 1 for element nodes and 3 for text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 20 '06 #2

P: n/a

Martin Honnen wrote:
Moses wrote:
The Value for childNodes.length differs with mozilla and IE
<track_order>
<status>0</status>
</track_order>

xmlDoc is the above XML

var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);

For Mozilla I get n.childNodes.length = 3

For IE I get n.childNodes.length = 1

Mozilla for XML and HTML document includes whitespace between elements
as text nodes in the DOM, IE does not do that for HTML at all and for
XML does not do it by default (although you can set preserveWhiteSpace =
true on an MSXML DOMDocument).
So for Mozilla that track_order element has as its first child node a
text node with white space, then the status element as the second child
and then again a text node with white space as the third child.
MSXML (used by IE) only has the element node.

That should not pose a problem at all, if you are looking for child
elements then ignore the childNodes collection and simply use
getElementsByTagName (e.g.
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
gives you the element) and/or XPath to access the child elements. If you
still think you need to loop through childNodes then include checks for
nodeType being 1 for element nodes and 3 for text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/


Hi

Thank you....

moses

Oct 20 '06 #3

P: n/a
Hi
I solved the problem by

xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]

Since I am new to XML and want to learn more

Can u please give some more details on your last point because I
didnt understood it.
If you still think you need to loop through childNodes then include
checks for
nodeType being 1 for element nodes and 3 for text nodes.

regards
moses
Moses wrote:
Martin Honnen wrote:
Moses wrote:
The Value for childNodes.length differs with mozilla and IE
<track_order>
<status>0</status>
</track_order>
>
xmlDoc is the above XML
>
var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);
>
For Mozilla I get n.childNodes.length = 3
>
For IE I get n.childNodes.length = 1
Mozilla for XML and HTML document includes whitespace between elements
as text nodes in the DOM, IE does not do that for HTML at all and for
XML does not do it by default (although you can set preserveWhiteSpace =
true on an MSXML DOMDocument).
So for Mozilla that track_order element has as its first child node a
text node with white space, then the status element as the second child
and then again a text node with white space as the third child.
MSXML (used by IE) only has the element node.

That should not pose a problem at all, if you are looking for child
elements then ignore the childNodes collection and simply use
getElementsByTagName (e.g.
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
gives you the element) and/or XPath to access the child elements. If you
still think you need to loop through childNodes then include checks for
nodeType being 1 for element nodes and 3 for text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Hi

Thank you....

moses
Oct 20 '06 #4

P: n/a
Moses wrote:
I solved the problem by

xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
Can u please give some more details on your last point because I
didnt understood it.
If you still think you need to loop through childNodes then include
checks for
nodeType being 1 for element nodes and 3 for text nodes.
Assuming you did e.g.
var orderElement = xmlDoc.getElementsByTagName('track_order')[0];
you could loop through the childNodes collection looking for element
nodes e.g.
if (orderElement != null) {
for (var i = 0, l = orderElement.childNodes.length; i < l; i++) {
var child = orderElement.childNodes[i];
if (child.nodeType === 1) {
// you have an element node here
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 20 '06 #5

P: n/a

Hi

Thank u very much for ur help I got it.......

regards
moses

Martin Honnen wrote:
Moses wrote:
I solved the problem by

xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]

Can u please give some more details on your last point because I
didnt understood it.
If you still think you need to loop through childNodes then include
checks for
nodeType being 1 for element nodes and 3 for text nodes.

Assuming you did e.g.
var orderElement = xmlDoc.getElementsByTagName('track_order')[0];
you could loop through the childNodes collection looking for element
nodes e.g.
if (orderElement != null) {
for (var i = 0, l = orderElement.childNodes.length; i < l; i++) {
var child = orderElement.childNodes[i];
if (child.nodeType === 1) {
// you have an element node here
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.