PHP | DOMDocument createAttribute() Function
Last Updated :
27 Aug, 2019
The DOMDocument::createAttribute() function is an inbuilt function in PHP which is used to create a new instance of class DOMAttr.
Syntax:
DOMAttr DOMDocument::createAttribute( string $name )
Parameters: This function accepts single parameter $name which holds the name of the attribute.
Return Value: This function returns the new DOMAttr object on success or FALSE on failure.
Below programs illustrate the DOMDocument::createAttribute() function in PHP:
Program 1:
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$domElement = $domDocument ->createElement( 'organization' ,
'A computer science portal' );
$domAttribute = $domDocument ->createAttribute( 'name' );
$domAttribute ->value = 'GeeksforGeeks' ;
$domElement ->appendChild( $domAttribute );
$domDocument ->appendChild( $domElement );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<organization name="GeeksforGeeks">A computer science portal</organization>
Program 2:
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$domElement = $domDocument ->createElement( 'organization' ,
'GeeksforGeeks' );
$domAttribute1 = $domDocument ->createAttribute( 'name' );
$domAttribute1 ->value = 'GeeksforGeeks' ;
$domElement ->appendChild( $domAttribute1 );
$domAttribute2 = $domDocument ->createAttribute( 'address' );
$domAttribute2 ->value = 'Noida' ;
$domElement ->appendChild( $domAttribute2 );
$domAttribute3 = $domDocument ->createAttribute( 'email' );
$domAttribute3 ->value = 'abc@geeksforgeeks.org' ;
$domElement ->appendChild( $domAttribute3 );
$domDocument ->appendChild( $domElement );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<organization name="GeeksforGeeks" address="Noida" email="abc@geeksforgeeks.org">
GeeksforGeeks
</organization>
Reference: https://www.php.net/manual/en/domdocument.createattribute.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...