faqts : Computers : Programming : Languages : JavaScript : XML : E4X (ECMAScript for XML) : XML objects

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

0 of 1 people (0%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

How do I use the method elements?
How do I use the method elements?

Apr 10th, 2005 02:44
Martin Honnen,


The elements method takes one optional argument, the name of elements
you are looking for, and returns an XMLList with all matching child
elements or with all child element if no argument was passed.
[Note: while the elements method exists to access child elements the
language also provides access to child elements as properties of the XML
object e.g.
  xmlObject.childElementName
  xmlObject['childElementName']
so while the elements method exists you might not need/use it but
instead prefer to access child elements as properties. See
  http://www.faqts.com/knowledge_base/view.phtml/aid/35106/fid/1762
for details.]
Here are some examples using the elements method:
  var deities = <deities>
    <god>Kibo</god>
    <devil>Xibo</devil>
  </deities>;
  var allElements = deities.elements();
  alert(allElements.length()); // shows 2
  var godList = deities.elements('god');
  alert(godList.length()); // shows 1
  var devilList = deities.elements('devil');
  alert(devilList.length()); // shows 1
  var demonList = deities.elements('demon');
  alert(demonList.length()); // shows 0
  var xhtmlDiv = <div xmlns="http://www.w3.org/1999/xhtml">
    <h1>Kibology</h1>
    <p>Kibology for all.</p>
  </div>;
  alert(xhtmlDiv.elements('*').length()); // shows 2
  alert(xhtmlDiv.elements('p').length()); 
  // shows 0 as child elements are in a namespace
  // check with qualified name
  alert(xhtmlDiv.elements(new QName('http://www.w3.org/1999/xhtml',
'p')).length());
  // shows 1