Name

XML.appendChild( ) Method — add a new child node to a node, or move an existing node

Availability

Flash 5

Synopsis

theNode.appendChild(childNode)

Arguments

childNode

An existing XML node object.

Description

The appendChild( ) method adds the specified childNode to theNode as theNode’s last child. We can use appendChild( ) to add a new node to an existing node, to move a node within a document, or to move a node between documents. In each of these cases, childNode must be a reference to a node object that already exists.

To add a new child node to an existing node, we must first create the new child node using createElement( ), createTextNode( ), or cloneNode( ) or by parsing XML source code into an XML object. For example, in the following code, we create a new P node and a new text node. We append the text node to the P node and then append the P node and its text node child to the top node of a document:

// Create a document
myDoc = new XML('<P>paragraph 1</P>');

// Create a P node and a text node
newP = myDoc.createElement("P");
newText = myDoc.createTextNode("paragraph 2");

// Append the text node to the P node
newP.appendChild(newText);

// Append the P node (including its text child) to myDoc
myDoc.appendChild(newP);

trace(myDoc);  // Displays: "<P>paragraph 1</P><P>paragraph 2</P>"

To move a node within a document, specify a childNode that is a reference to an existing node in the document. In this situation, childNode indicates the old location of the node, and theNode indicates ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.