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.
To get all child nodes, including non-element nodes like text and comment nodes, use Node. childNodes .
Definition and Usage The childNodes property returns a NodeList of child nodes for the document.
XML DOM removeChild() Method The removeChild() method removes a specified child node from the current node. Tip: The removed child node can be inserted later into any element in the same document.
Child nodes include elements, text and comments. Note: The NodeList being live means that its content is changed each time new children are added or removed. The items in the collection of nodes are objects, not strings. To get data from node objects, use their properties.
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With