PHP | DOMDocument saveXML() Function
Last Updated :
29 Mar, 2023
The DOMDocument::saveXML() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after building a new dom document from scratch.
Syntax:
string DOMDocument::saveXML( DOMNode $node, int $options = 0 )
Parameters: This function accepts two parameters as mentioned above and described below:
- $node: This parameter is used for output only for specific node without XML declaration rather than the entire document.
- $options: This parameter adds the additional options. Currently this parameter supports only LIBXML_NOEMPTYTAG.
Return Value: This function returns the XML document on success or FALSE on failure.
Below programs illustrate the DOMDocument::saveXML() function in PHP:
Program 1:
php
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$domTN = $domDocument ->createTextNode( 'GeeksforGeeks' );
$domDocument ->appendChild( $domTN );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
GeeksforGeeks
Program 2:
php
<?php
$domDocument = new DOMDocument( '1.0' , 'iso-8859-1' );
$domElement = $domDocument ->createElement( 'organization' ,
'GeeksforGeeks' );
$domDocument ->appendChild( $domElement );
echo $domDocument ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<organization>GeeksforGeeks</organization>
Reference: https://www.php.net/manual/en/domdocument.savexml.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...