PHP | DOMDocument createElement() Function
Last Updated :
27 Aug, 2019
The DOMDocument::createElement() function is an inbuilt function in PHP which is used to create a new instance of class DOMElement.
Syntax:
DOMElement DOMDocument::createElement( string $name, string $value )
Parameters: This function accepts two parameters as mentioned above and described below:
- $name: This parameter holds the tag name of the element.
- $value: This parameter holds the value of the element. The default value of this function creates an empty element. The value of element can be set later using DOMElement::$nodeValue.
Return Value: This function returns a new instance of class DOMElement on success or FALSE on failure.
Below programs illustrate the DOMDocument::createElement() function in PHP:
Program 1:
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$domElement = $domDocument ->createElement( 'organization' , 'GeeksforGeeks' );
$domDocument ->appendChild( $domElement );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<organization>GeeksforGeeks</organization>
Program 2:
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$domElement1 = $domDocument ->createElement( 'organization' );
$domElement2 = $domDocument ->createElement( 'name' , 'GeeksforGeeks' );
$domElement3 = $domDocument ->createElement( 'address' , 'Noida' );
$domElement4 = $domDocument ->createElement( 'email' , 'abc@geeksforgeeks.org' );
$domDocument ->appendChild( $domElement1 );
$domElement1 ->appendChild( $domElement2 );
$domElement1 ->appendChild( $domElement3 );
$domElement1 ->appendChild( $domElement4 );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<organization>
<name>GeeksforGeeks</name>
<address>Noida</address>
<email>abc@geeksforgeeks.org</email>
</organization>
Reference: https://www.php.net/manual/en/domdocument.createelement.php
Share your thoughts in the comments
Please Login to comment...