PHP | DOMNode appendChild() function

The DOMNode::appendChild() function is an inbuilt function in PHP which is used to appends a child to an existing list of children or creates a new list of children. The child can be created with DOMDocument::createElement(), DOMDocument::createTextNode() or by using any other node.

Syntax:

DOMNode DOMNode::appendChild( DOMNode $newnode )

Parameters:This function accepts a single parameter $newnode which holds the node to append.

Return Value: This function returns the node which is added.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly or if the previous parent of the node being inserted is readonly or DOM_HIERARCHY_REQUEST_ERR, if the node is of a type that does not allow children of the type of the $newnode node, or if the node to append is one of this node’s ancestors or this node itself or DOM_WRONG_DOCUMENT_ERR, if $newnode was created from a different document than the one that created this node.



Below given programs illustrate the DOMNode::appendChild() function in PHP:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new DOMDocument
$doc = new DOMDocument();
    
// Create an Element
$node = $doc->createElement("em", "GeeksforGeeks");
  
// Append the child
$newnode = $doc->appendChild($node);
  
// Render the XML
echo $doc->saveXML();
?>

chevron_right


Output:

GeeksforGeeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new DOMDocument
$doc = new DOMDocument();
   
// Create an Table element
$table = $doc->createElement("table");
   
// Append the child
$tablenode = $doc->appendChild($table);
   
// Create a tr element
$tr = $doc->createElement("tr");
   
// Append the child
$tablenode->appendChild($tr);
   
// Create a th element
$th = $doc->createElement("th", "Name");
   
// Set the attribute
$th->setAttribute("style", "border: 1px solid #dddddd;");
   
// Append the child
$tr->appendChild($th);
   
// Create a tr element
$tr = $doc->createElement("tr");
   
// Append the child
$tablenode->appendChild($tr);
   
// Create a th element
$th = $doc->createElement("td", "GeeksforGeeks");
   
// Set the attribute
$th->setAttribute("style", "background-color: #dddddd;border: 1px solid #dddddd;");
   
// Append the child
$tr->appendChild($th);
   
// Render the XML
echo $doc->saveXML();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/domnode.appendchild.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.