The DOMImplementation::createDocument() function is an inbuilt function in PHP which is used to create a DOMDocument object of the specified type with its document element.
Syntax:
php
Output:
Example 2:
php
Output:
Reference: https://www.php.net/manual/en/domimplementation.createdocument.php
DOMDocument DOMImplementation::createDocument( string $namespaceURI = NULL, string $qualifiedName = NULL, DOMDocumentType $doctype = NULL )Parameters: This function accepts three parameters as mentioned above and described below:
- $namespaceURI (Optional): It specifies the namespace URI of the document element to create.
- $qualifiedName (Optional): It specifies the qualified name of the document element to create.
- $doctype (Optional): It specifies the type of document to create or NULL.
<?php
// Create a DOMImplementation instance
$documentImplementation = new DOMImplementation();
// Create a document
$document = $documentImplementation->createDocument(null, 'html');
// Get the document element
$html = $document->documentElement;
// Create a HTML element
$head = $document->createElement('html');
// Create a strong element
$title = $document->createElement('strong');
$text = $document->createTextNode('GeeksforGeeks');
$body = $document->createElement('body');
// Append the children
$title->appendChild($text);
$head->appendChild($title);
$html->appendChild($head);
$html->appendChild($body);
// Render the XML
echo $document->saveXML();
?>
Example 2:
<?php
// Create a DOMImplementation instance
$documentImplementation = new DOMImplementation();
// Create a document
$document = $documentImplementation->createDocument(null, 'html');
// Get the document element
$html = $document->documentElement;
// Create a HTML element
$head = $document->createElement('html');
// Create a paragraph element
$p = $document->createElement('p');
// Create a text node
$text = $document->createTextNode('GeeksforGeeks');
// Append the children
$p->appendChild($text);
// Set the CSS using attribute
$p->setAttribute('style', 'color:blue;font-size:100px');
// Append paragraph to the head of document
$head->appendChild($p);
// Append head to the html
$html->appendChild($head);
// Render the XML
echo $document->saveXML();
?>
Reference: https://www.php.net/manual/en/domimplementation.createdocument.php