PHP | DOMDocument createAttribute() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createElement() function to create an element node
$domElement = $domDocument->createElement('organization',
                             'A computer science portal');
  
// Use createAttribute() function to create an attribute node
$domAttribute = $domDocument->createAttribute('name');
  
// Value of created attribute
$domAttribute->value = 'GeeksforGeeks';
  
// Append element to the document
$domElement->appendChild($domAttribute);
  
// Append it to the document itself
$domDocument->appendChild($domElement);
  
// Save the document into XML and display it
echo $domDocument->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<organization name="GeeksforGeeks">A computer science portal</organization>

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createElement() function to create an element node
$domElement = $domDocument->createElement('organization',
                                         'GeeksforGeeks');
  
$domAttribute1 = $domDocument->createAttribute('name');
  
// Value for the created attribute
$domAttribute1->value = 'GeeksforGeeks';
  
// Append element to the document
$domElement->appendChild($domAttribute1);
  
// Use createAttribute() function to create an attribute node
$domAttribute2 = $domDocument->createAttribute('address');
  
// Value for the created attribute
$domAttribute2->value = 'Noida';
  
// Append element to the document
$domElement->appendChild($domAttribute2);
  
// Use createAttribute() function to create an attribute node
$domAttribute3 = $domDocument->createAttribute('email');
  
// Value for the created attribute
$domAttribute3->value = 'abc@geeksforgeeks.org';
  
// Append element to the document
$domElement->appendChild($domAttribute3);
  
// Append it to the document itself
$domDocument->appendChild($domElement);
  
// Save the document to XML and display it
echo $domDocument->saveXML();
  
?>

chevron_right


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



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.