PHP | SimpleXMLElement saveXML() Function Last Updated : 27 Sep, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: php <?php // Loading XML document to $user $user = <<<XML <user> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> </user> XML; // Creating new SimpleXMLElement object from $user $xml = new SimpleXMLElement($user); // Printing as XML echo $xml->saveXML(); echo $xml->saveXML('savexmltofile.xml'); ?> Output: user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India. 1 Saved XML file: Program 2: Save XML filename by using sample.xml html <?xml version="1.0"?> <user> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> </user> index.php php <?php // Loading XML document from sample.xml to $user // and creating new SimpleXMLElement object $xml = new SimpleXMLElement("sample.xml", 0, TRUE); // Printing data as xml document echo $xml->saveXML(); echo $xml->saveXML('savexmltofile.xml'); ?> Output: user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India. 1 saved XML file: Reference: https://www.php.net/manual/en/simplexmlelement.asxml.php Comment More infoAdvertise with us Next Article PHP | SimpleXMLElement addChild() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Php-SimpleXML Similar Reads PHP | SimpleXMLElement asXML() Function Pre-requisite: Read XML The SimpleXMLElement::asXML() function is an inbuilt function in PHP which returns well-formed XML string from a SimpleXML object. Syntax: mixed SimpleXMLElement::asXML( $filename ) Parameters: This function accepts single parameter $filename which is optional. It specified t 2 min read PHP | SimpleXMLElement XPath() Function Pre-requisite: Read XML Basics The SimpleXMLElement::xpath() function is an inbuilt function in PHP which runs XPath query on the XML document. Syntax: SimpleXMLElement::xpath( $path ) Parameters: This function accepts single parameter $path which is required. It is used to specify the XPath path of 2 min read PHP | SimpleXMLElement::getName() Function Pre-requisite: Read XML basicsThe SimpleXMLElement::getName() function is an inbuilt function in PHP which returns the name of the xml element.Syntax:  string SimpleXMLElement::getName( void ) Parameter: This function does not accept any parameter.Return Value: It returns a string which represents 2 min read PHP | SimpleXMLElement addChild() Function Pre-requisite: Read XML Basics The SimpleXMLElement::addChild() function is an inbuilt function in PHP which is used to add a child in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::addChild($name, $value, $namespace); Parameter: This function accepts three parameters as mentioned ab 1 min read PHP | SimpleXMLElement __toString() Function The XMLReader::__toString() function is an inbuilt function in PHP which is used to get the text content that is directly in current element. This function does not return text content that is inside this element's children. Syntax: void XMLReader::__toString( void ) Parameters: This function doesn 1 min read PHP | SimpleXMLElement children() Function Pre-requisite: Read XML BasicsThe SimpleXMLElement::children() function is an inbuilt function in PHP which returns children of a given node in a SimpleXML object. Syntax:  SimpleXMLElement SimpleXMLElement::children( $namespace, $is_prefix ) Parameter: This function accepts two parameters as ment 3 min read Like