The
SimpleXMLElement::saveXML() function is an inbuilt function in PHP which return well-formed XML string from a SimpleXML object. It is an alias of SimpleXMLElement::asXML() function.
Syntax:
mixed SimpleXMLElement::saveXML( string $filename )
Parameter: This function accepts single parameter
$filename which is optional. It specified this function save data to the file instead of returning as XML.
Return Value: This function returns a string representing the data and filename if it is specified on success or returns False on failure.
Note: This function is available for PHP 5.0.1 and newer version.
Below programs illustrate the SimpleXMLElement::saveXML() function in PHP:
Program 1:
Output:
user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India. 1
Saved XML file:
👁 Image
Program 2: Save XML filename by using
sample.xml
index.php
Output:
user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India. 1
saved XML file:
👁 Image
Reference: https://www.php.net/manual/en/simplexmlelement.asxml.php