DOMNode::appendChild

(PHP 5, PHP 7, PHP 8)

DOMNode::appendChild Adds new child at the end of the children

Description

public DOMNode::appendChild(DOMNode $node): DOMNode|false

This function appends a child to an existing list of children or creates a new list of children. The child can be created with e.g. DOMDocument::createElement(), DOMDocument::createTextNode() etc. or simply by using any other node.

When using an existing node it will be moved.

Parameters

node

The appended child.

Return Values

The node added or false on error.

Errors/Exceptions

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

DOM_HIERARCHY_REQUEST_ERR

Raised if this node is of a type that does not allow children of the type of the node node, or if the node to append is one of this node's ancestors or this node itself.

DOM_WRONG_DOCUMENT_ERR

Raised if node was created from a different document than the one that created this node.

Examples

The following example will add a new element node to a fresh document.

Example #1 Adding a child

<?php

$doc
= new DOMDocument;

$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);

echo
$doc->saveXML();
?>

Example #2 Nested children

<?php

$doc
= new DOMDocument;

$headNode = $doc->createElement("head");
$doc->appendChild($headNode);

$titleNode = $doc->createElement("title");
$headNode->appendChild($titleNode);

echo
$doc->saveXML();
?>

See Also

add a note add a note

User Contributed Notes 4 notes

up
18
krisdover at hotmail dot com
16 years ago
What's not mentioned here is that DOMNode::appendChild() can also be used to move an existing node to another part of the DOMDocument, e.g.

<?php
$doc
= new DOMDocument();
$doc->loadXML("<foobar><bar/><foo/></foobar>");
$bar = $doc->documentElement->firstChild;
$foo = $doc->documentElement->lastChild;
$foo->appendChild($bar);
print
$doc->saveXML();
?>

This produces:

<?xml version="1.0"?>
<foobar><foo><bar/></foo></foobar>

Note that the nodes "<foo/>" and "<bar/>" were siblings, i.e. the first and last child of "<foobar>" but using appendChild() we were able to move "<bar/>" so that it is a child of "<foo/>".

This saves you the trouble of doing a DOMNode::removeChild($bar) to remove "<bar/>" before appending it as a child of "<foo/>".

Kris Dover
up
7
cweiske at php dot net
7 years ago
If you want to move the children of one node to another, you cannot simply iterate on $element->childNodes - you have to make an array first:

<?php
$children
= [];
foreach (
$elemWithChildren->childNodes as $child) {
   
$children[] = $child;
}
foreach (
$children as $child) {
   
$targetElement->appendChild($child);
}
?>
up
3
jrtayloriv at gmail dot com
16 years ago
If you want to create nested DOM elements:

<?php
    $doc
= new DOMDocument();
   
   
$foo = $doc->createElement("foo");
   
$doc->appendChild($foo);

   
$bar = $doc->createElement("bar");
   
$foo->appendChild($bar);

   
$bazz = $doc->createElement("bazz");
   
$foo->appendChild($bazz);

    echo
$doc->saveXML();
?>

Is equivalent to:

<foo>
  <bar></bar>
  <bazz></bazz>
</foo>
up
1
frame at dynamiccreated dot de
13 years ago
Aware dealing with DOMNodeList and appendChild() on the same Node.

If you want to replace only the children not the DOMElement itself you probably foreach childNodes-property or get the DOMElements with a for-loop and item()-method of the DOMNodeList.

You will fail if you not clone the received single DOMElement. Actually the count of the DOMNodelist will be decreased on appendChild count but appendChild seems to refer to the old Nodelist and nothing visible will happen. Cloning will help.
To Top