Entry
How do I use the method child?
How do I use the method child?
Apr 9th, 2005 03:18
Martin Honnen,
The method child takes one argument with a property name and returns an
XMLList with children matching the property name. Thus if the property
name is a positive integer (including 0) then the child with that index
is returned (to be exactly an XMLList containing that child only),
otherwise the children matching the property name are returned.
[Note: the children of an XML object can be accessed like normal
properties e.g.
xmlObject.propertyName
xmlObject['propertyName']
thus while there is a method child you might not need/use it but instead
prefer the shorter property access, see
http://www.faqts.com/knowledge_base/view.phtml/aid/35106/fid/1762
for details.]
Here are some examples using the method child:
XML.ignoreComments = false;
var gods = <gods>
<god>Kibo</god>
<god>Xibo</god>
<!-- Kibology for all. -->
</gods>;
var god0 = gods.child(0);
alert(god0); // shows 'Kibo';
var god1 = gods.child(1);
alert(god1); // shows 'Xibo'
alert(gods.child(2)); // shows '<!-- Kibology for all. -->'
alert(gods.child(3)); // shows 'undefined'
var godList = gods.child('god');
alert(godList.length()); // shows 2
// all child nodes with wild card '*'
alert(gods.child('*').length()); // shows 3