PHP | DOMElement __construct() Function
Last Updated :
20 Feb, 2020
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:
<?php
$dom = new DOMDocument();
$element = $dom ->appendChild( new DOMElement( 'root' ));
$element_new = new DOMElement( 'h1' ,
$element ->appendChild( $element_new );
echo $dom ->saveXML();
?>
|
Output:
<?xml version="1.0"?>
<root><h1 xmlns="http://sample_url">Heading</h1></root>
Program 2:
<?php
$dom = new DOMDocument();
$element = $dom ->appendChild( new DOMElement( 'root' ));
$element_mark = new DOMElement( 'mark' , 'Marked' );
$element ->appendChild( $element_mark );
$element_break = new DOMElement( 'br' );
$element ->appendChild( $element_break );
$element_delete = new DOMElement( 'del' , 'Deleted' );
$element ->appendChild( $element_delete );
$element_break = new DOMElement( 'br' );
$element ->appendChild( $element_break );
$element_bold = new DOMElement( 'b' , 'Bold' );
$element ->appendChild( $element_bold );
echo $dom ->saveXML();
?>
|
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
Share your thoughts in the comments
Please Login to comment...