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
$documentImplementation = new DOMImplementation();
$document = $documentImplementation ->createDocument(null, 'html' );
$html = $document ->documentElement;
$head = $document ->createElement( 'html' );
$title = $document ->createElement( 'strong' );
$text = $document ->createTextNode( 'GeeksforGeeks' );
$body = $document ->createElement( 'body' );
$title ->appendChild( $text );
$head ->appendChild( $title );
$html ->appendChild( $head );
$html ->appendChild( $body );
echo $document ->saveXML();
?>
|
Output:
Example 2:
<?php
$documentImplementation = new DOMImplementation();
$document = $documentImplementation ->createDocument(null, 'html' );
$html = $document ->documentElement;
$head = $document ->createElement( 'html' );
$p = $document ->createElement( 'p' );
$text = $document ->createTextNode( 'GeeksforGeeks' );
$p ->appendChild( $text );
$p ->setAttribute( 'style' , 'color:blue;font-size:100px' );
$head ->appendChild( $p );
$html ->appendChild( $head );
echo $document ->saveXML();
?>
|
Output:
Reference: https://www.php.net/manual/en/domimplementation.createdocument.php
Last Updated :
26 Feb, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...