PHP | DOMDocument createElementNS() Function

The DOMDocument::createElementNS() function is an inbuilt function in PHP which is used to create a new element node with an associated namespace.

Syntax:

DOMElement DOMDocument::createElementNS( string $namespaceURI, 
                             string $qualifiedName, string $value )

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

  • $namespaceURI: This parameter holds the URI of the namespace.
  • $qualifiedName: This parameter holds the qualified name of the element, as prefix:tagname.
  • $value: This parameter holds the value of the element. The default value of this parameter is empty or none, means an empty element created.

Return Value: This function returns the new DOMElement on success or FALSE on failure.

Below programs illustrate the DOMDocument::createElementNS() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$dom = new DOMDocument('1.0', 'utf-8');
  
// Use createElementNS() function to create new
// element node with an associated namespace
$element = $dom->createElementNS('https://www.geeksforgeeks.org/php',
        'php:function', 'Welcome to GeeksforGeeks');
  
// Append the child element
$dom->appendChild($element);
  
// Create XML document and diplsy it
echo $dom->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="utf-8"?>
<php:function xmlns:php="https://www.geeksforgeeks.org/php">
    Welcome to GeeksforGeeks
</php:function>

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$dom = new DOMDocument('1.0', 'utf-8');
  
// Use createElementNS() function to create new
// element node with an associated namespace
$element1 = $dom->createElementNS('https://www.geeksforgeeks.org/php',
        'organization:GeeksforGeeks', 'A computer science portal');
          
$element2 = $dom->createElementNS('https://www.geeks.org/html',
        'php:link', 'Welcome to GeeksforGeeks');
          
$element3 = $dom->createElementNS('https://www.geeksforgeeks.org/algo',
        'algo:link', 'Best coding platform');
  
// Append the child element
$dom->appendChild($element1);
$dom->appendChild($element2);
$dom->appendChild($element3);
  
// Create XML document and diplsy it
echo $dom->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="utf-8"?>
<organization:GeeksforGeeks xmlns:organization
        ="https://www.geeksforgeeks.org/php">
    A computer science portal
</organization:GeeksforGeeks>

<php:link xmlns:php="https://www.geeks.org/html">
    Welcome to GeeksforGeeks
</php:link>

<algo:link xmlns:algo="https://www.geeksforgeeks.org/algo">
    Best coding platform
</algo:link>

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