Reputation: 11336
I have the following XML sitting in a var called RoomPriceInfo in javascript:
<?xml version="1.0" encoding="UTF-8"?>
<BkgItemHotelRoomPrices CurrCode="EUR">
<RoomType Code="DB" Count="1" Desc="Double" Age="0">
<PriceInfo EndDate="2011-12-17" AgentMarkup="0.0" MarkupPerc="0.1075" FitRdg="0.25" MarkupPrice="48.73" AgentPrice="48.75" StartDate="2011-12-11" Nights="7" FitRdgPrice="48.75" CurrDec="2" CurrDecPrice="48.75" SuppPrice="44.0"/>
</RoomType>
</BkgItemHotelRoomPrices>
and the following code:
DBRoomPrice = RoomPriceInfo.doXPath("//RoomType[@Code='DB']");
alert(DBRoomPrice[0].children.length);
Under FF7 on Ubuntu and FF8 on WinXP I get an alert of 1 which is correct. However under IE8 on WinXP and IE9 on Windows 7 nothing happens. It just dies silently.
Please can anyone shed any light on this? If I do a getElementById
on the DOM object and then ask for children on that, then IE8 & IE9 behave correctly.
Upvotes: 6
Views: 3625
Reputation: 348992
Internet Explorer (including version 11!) does not support the .children
property om XML elements.
If you want to get the number of child elements, use element.childElementCount
(IE9+):
element.children.length; // Does not work in IE on XML elements
element.childElementCount; // Works in every browser
If you merely want to know whether an element has any children, you can also check whether element.firstElementChild
(or element.lastElementChild
) is not null. This property is supported in IE9+:
element.children.length === 0; // All real browsers
element.firstElementChild !== null; // IE 9+
If you want to iterate through all child elements of the XML node, use childNodes
and exclude the non-element nodes via their nodeType
:
for (var i = 0, len = element.childNodes.length; i < l; ++i) {
var child = element.childNodes[i];
if (child.nodeType !== 1/*Node.ELEMENT_NODE*/) continue;
// Now, do whatever you want with the child element.
}
Upvotes: 15
Reputation: 51
It may not solve the problem but.. you should use childNodes instead of children property to access the children nodes. I'm not sure which one is better, but I know for sure childNodes is wide supported.. may be Microsoft did it also like this?!
Upvotes: 0