PHP | DOMDocument saveXML() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createTextNode() function to create a text node
$domTN = $domDocument->createTextNode('GeeksforGeeks');
  
// Append element to the document
$domDocument->appendChild($domTN);
  
// Use saveXML() function to save the XML document
echo $domDocument->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
GeeksforGeeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createElement() function to create an element node
$domElement = $domDocument->createElement('organization',
                                       'GeeksforGeeks');
  
// Append element to the document
$domDocument->appendChild($domElement);
  
// Use saveXML() function to create a XML document
echo $domDocument->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<organization>GeeksforGeeks</organization>

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