Mozilla Vs IE for childNodes.length

M

Moses

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
 
M

Martin Honnen

Moses said:
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.
 
M

Moses

Martin said:
Moses said:
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.



Hi

Thank you....

moses
 
M

Moses

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

Martin said:
Moses said:
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.



Hi

Thank you....

moses
 
M

Martin Honnen

Moses said:
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;
if (child.nodeType === 1) {
// you have an element node here
}
}
}
 
M

Moses

Hi

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

regards
moses



Martin said:
Moses said:
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;
if (child.nodeType === 1) {
// you have an element node here
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top