Thursday, October 23, 2025
HomeLanguagesPHP | DOMDocument saveXML() Function

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: 

php




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


Output:

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

Program 2: 

php




<?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',
                                       'neveropen');
 
// Append element to the document
$domDocument->appendChild($domElement);
 
// Use saveXML() function to save a XML document
echo $domDocument->saveXML();
 
?>


Output:

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

Reference: https://www.php.net/manual/en/domdocument.savexml.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS