Entry
How do I use the method elements?
How do I use the method elements?
Apr 15th, 2005 04:49
Martin Honnen,
The method elements takes one optional argument, the element name to
look for, and returns an XMLList with the matching element children of
all XML objects in the original XMLList it is called on.
If the element name argument is not passed in then the wild card name
'*' is used so that an XMLList with all child elements of all XML
objects in the original XMLList is returned.
[Note: instead of using the elements method e.g.
xmlList.elements('elementName')
you can also use the shorter property accessor
xmlList.elementName
]
Here are some examples of using the method elements:
var html = <html lang="en">
<head>
<title>Kibology</title>
</head>
<body>
<h1>Kibology</h1>
<p>All for Kibology.</p>
<p>Kibology for all.</p>
</body>
</html>;
var xmlList = html.*;
// all child elements of the list:
alert(xmlList.elements().length()); // shows 4
for each (var xmlObject in xmlList.elements()) {
alert(xmlObject.nodeKind() + ': ' + xmlObject.name());
}
/* shows
'element: title'
'element: h1'
'element: p'
'element: p'
*/
alert(xmlList.elements('p').length()); // shows 2
alert(xmlList.elements('div').length()); // shows 0