PHP | DOMElement __construct() Function

The DOMElement::__construct() function is an inbuilt function in PHP which is used to create a new DOMElement object. This object is read-only and may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document.

Syntax:

public DOMElement::__construct( string $name, string $value, string $namespaceURI )

Parameters: This function accepts three parameters as mentioned above and described below:



  • $name: It specifies the tag name of the element.
  • $value (Optional): It specifies the value of the element.
  • $namespaceURI (Optional): It specifies the namespace URI to create the element within a specific namespace.

Below given programs illustrate the DOMElement::__construct() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Append a new Child which is a DOMElement
$element = $dom->appendChild(new DOMElement('root'));
  
// Create another h1 element using
// DOMElement  constructor
$element_new = new DOMElement('h1',
          'Heading', 'http://sample_url');
  
// Append the child
$element->appendChild($element_new);
  
// Save the XML
echo $dom->saveXML(); 
?>

chevron_right


Output:

<?xml version="1.0"?>
<root><h1 xmlns="http://sample_url">Heading</h1></root>

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Append a new Child which is a DOMElement
$element = $dom->appendChild(new DOMElement('root'));
  
// Create another DOMElement for mark
$element_mark = new DOMElement('mark', 'Marked');
  
// Append the child
$element->appendChild($element_mark);
  
// Create another DOMElement for break
$element_break = new DOMElement('br');
  
// Append the child
$element->appendChild($element_break);
  
// Create another DOMElement for delete
$element_delete = new DOMElement('del', 'Deleted');
  
// Append the child
$element->appendChild($element_delete);
  
// Create another DOMElement for break
$element_break = new DOMElement('br');
  
// Append the child
$element->appendChild($element_break);
  
// Create another DOMElement for bold
$element_bold = new DOMElement('b', 'Bold');
  
// Append the child
$element->appendChild($element_bold);
  
// Save the XML
echo $dom->saveXML(); 
?>

chevron_right


Output:

<?xml version="1.0"?>
<root>
    <mark>Marked</mark><br/>
    <del>Deleted</del><br/>
    <b>Bold</b>
</root>

Reference: https://www.php.net/manual/en/domelement.construct.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.