I have a dream... Increase my knowledge
Showing posts with label parent. Show all posts
Showing posts with label parent. Show all posts

Accessing descendants

var xml:XML = <LOAN>
<BOOK ISBN="976568989" DATE="1136091600000">
<TITLE>Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>My Books</PUBLISHER>
</BOOK>

<DVD ISBN="5432334456" DATE="1136691000000">
<TITLE>2001 A Space Odysses</TITLE>
<DIRECTOR>Stanley Kubrick</DIRECTOR>
<PUBLISHER>Warner</PUBLISHER>
</DVD>

<DVD ISBN="7878785566" DATE="1136691000000">
<TITLE>Toy Story</TITLE>
<DIRECTOR>John Lasseter</DIRECTOR>
<PUBLISHER>Walt Disney</PUBLISHER>
</DVD>
</LOAN>

theElement..identifier

A descendant operator(..) returns a XMLList representing all descendants of theElement whose name match identifier.

i.e. xml..DIRECTOR
//Notice that the <DIRECTOR> elements are not direct children of the <LOAN> element; they are grandchildren.
Above expression yields an XMLList that has two XML instances, representing the two <DIRECTOR> elements.


Example
for each(var title:XML in load..TITLE)
trace(title);
//Output
Ulysses
2001 A Space Odysses
Toy Story

theElement..@attributeName

A descendant operator also works with attributes(..@) returns a XMLList representing all descendants attributes of theElement whose name match attributeName.

i.e. xml..@DUE
Above expression yields an XMLList that has three XML instances, representing the three DUE attributes.

 

From the previous example

var pub:XML = novel.PUBLISHER[0];
pub.parent();
//Reference to <PUBLISHER>'s parent (which is <BOOK>)

If the pub is a root node (not in this case) the parent() method returns null. (i.e. novel.parent();)


Accessing Attributes
novel.attributes() and novel.@*
//Returns an XMLList representing <BOOK>'s attributes (which is ISBN node)

Normally attributes are accessed by name, using either the attribute() method
someElement.attribute("attributeName")
or E4X's more convenient variable-access syntax
someElement.@attributeName

 

Example:

var novel:XML = <BOOK ISBN="0141182806>
<TITLE>Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Hi Books</PUBLISHER>
<DESCRIPTION>A <B>very</B> thick book.</DESCRIPTION>
</BOOK>;

novel.children() and novel.*
//Returns an XMLList representing 's child nodes

novel.children()[1]
//Reference to 's second child node

novel.child("AUTHOR") and novel.AUTHOR
//Returns all child elements of named "AUTHOR"

novel.AUTHOR[0].setName("WRITER")
//Change name of the first child element named "AUTHOR"

novel.TITLE.children()[0] and novel.TITLE.*[0]
//A reference to the text node Ulysses and not the string "Ulysses"

novel.TITLE.*[0].parent() //A reference to the <TITLE> element

novel.TITLE.*[0].nodeKind() //Returns the string "text"

novel.TITLE.*[0].toString() //Returns the string "Ulysses"

The <DESCRIPTION> element contains both element and text child nodes:

  • A (text node);
  • <B>very</B> (element node with a child text node);
  • thick book. (text node);

The text() method can also used to retrieve the text nodes from an entire XMLList, not just a single XML element.
trace(novel.DESCRIPTION.text()[0]); //Display: A

trace(novel.DESCRIPTION.text()[1]); //Display: thick book