Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | DOMImplementation createDocument() Function

PHP | DOMImplementation createDocument() Function

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:

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.

Return Value: This function returns DOMDocument object on success.

Exceptions: This function throws DOM_WRONG_DOCUMENT_ERR, if doctype has already been used with a different document or was created from a different implementation or DOM_NAMESPACE_ERR, if there is an error with the namespace, as determined by $namespaceURI and $qualifiedName.

Below examples illustrate the DOMImplementation::createDocument() function in PHP:

Example 1:




<?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('neveropen');
$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();
?>


Output:

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('neveropen');
   
// 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();
?>


Output:

Reference: https://www.php.net/manual/en/domimplementation.createdocument.php

RELATED ARTICLES

Most Popular

Recent Comments