PHP | DOMDocument save() Function Last Updated : 29 Aug, 2019 Comments Improve Suggest changes Like Article Like Report The DOMDocument::save() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after creating the new dom document from scratch. Syntax: int DOMDocument::save( string $filename, int $options = 0 ) Parameters: This function accepts two parameters as mentioned above and described below: $filename: This parameter holds the path to save the XML document. $options: This parameter holds the additional options. Currently this parameter supports only LIBXML_NOEMPTYTAG. Return Value: This function returns the number of bytes written on success or FALSE on failure. Below programs illustrate the DOMDocument::save() function in PHP: Program 1: php <?php // Create a new DOMDocument $domDocument = new DOMDocument('1.0', 'iso-8859-1'); // Set the formatOutput to true $domDocument->formatOutput = true; // Create an element $domElement = $domDocument->createElement('organization', 'GeeksforGeeks'); // Append element to the document $domDocument->appendChild($domElement); // Save the XML document $domDocument->save("abcd.xml"); echo "File saved successfully"; ?> Output: File saved successfully The content of saved file abcd.xml: <?xml version="1.0" encoding="iso-8859-1"?> <organization>GeeksforGeeks</organization> Program 2: php <?php // Create a new DOMDocument $domDocument = new DOMDocument('1.0', 'iso-8859-1'); // Create an element $domElement1 = $domDocument->createElement('organization'); $domElement2 = $domDocument->createElement('name', 'GeeksforGeeks'); $domElement3 = $domDocument->createElement('address', 'Noida'); $domElement4 = $domDocument->createElement('email', '[email protected]'); // Append element to the document $domDocument->appendChild($domElement1); $domElement1->appendChild($domElement2); $domElement1->appendChild($domElement3); $domElement1->appendChild($domElement4); // Save the XML file $domDocument->save("g4g.xml"); echo "File saved successfully"; ?> Output: File saved successfully The content of saved file g4g.xml: <?xml version="1.0" encoding="iso-8859-1"?> <organization> <name>GeeksforGeeks</name> <address>Noida</address> <email>[email protected]</email> </organization> Reference: https://www.php.net/manual/en/domdocument.save.php Comment More infoAdvertise with us Next Article PHP | DOMDocument save() Function jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads 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 functio 2 min read PHP DOMDocument saveHTML() Function The DOMDocument::saveHTML() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after building the dom document from scratch. Syntax: string DOMDocument::saveHTML( DOMNode $node = NULL ) Parameters: This function accepts s 2 min read PHP DOMDocument saveHTMLFile() Function The DOMDocument::saveHTMLFile() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after creating the dom document. Syntax: int DOMDocument::saveHTMLFile( string $filename ) Parameters: This function accepts a single para 2 min read PHP | DOMDocument validate() Function The DOMDocument::validate() function is an inbuilt function in PHP which is used to validate the document based on its DTD (Document Type Definition). DTD defines the rules or structure to be followed by the XML file and if a XML document doesn't follows this format then this function will return fa 2 min read PHP | DOMDocument schemaValidate() Function The DOMDocument::schemaValidate() function is an inbuilt function in PHP which is used to validate a document based on the given schema file. The schema file can be in an XSD format which is the recommendation from W3C (World Wide Web Consortium). Syntax: bool DOMDocument::schemaValidate( string $fi 2 min read Like