saveXML()
SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLElement::saveXML() function is one of the
Introduction
SimpleXML is a PHP extension that provides a simple, easy-to-use API for working with XML documents. The SimpleXMLElement::saveXML() method is the standard way to turn a SimpleXMLElement object back into XML — either as a string in memory or as a file on disk.
You typically reach for saveXML() at the end of a workflow: after you have loaded XML (with simplexml_load_string()) or built it node by node (with addChild()), saveXML() serializes the result so you can return it, store it, or send it over the network.
This article covers the method's signature, how it differs from asXML(), and practical recipes including pretty-printing.
Syntax
public SimpleXMLElement::saveXML(?string $filename = null): string|false$filename— Optional. If provided, the XML is written to this file and the method returnstrueon success orfalseon failure. The target directory must already exist and be writable by the PHP process. If omitted (the default), the method returns the XML as a string instead of writing a file.- Return value — A
stringcontaining the serialized XML when$filenameis omitted; aboolindicating success when$filenameis given. Returnsfalseonly on failure.
SimpleXMLElement::saveXML()is an alias ofSimpleXMLElement::asXML()— the two methods are interchangeable and accept the same single optional$filenameargument. Note that, unlikeDOMDocument::saveXML(), the SimpleXML version does not accept an$optionsargument; passing one throws anArgumentCountError.
Saving XML to a string
Call saveXML() with no arguments to get the document as a string:
This prints the full document, including the XML declaration:
<?xml version="1.0"?>
<book><title>PHP Basics</title></book>We create a SimpleXMLElement representing a book element with a title child, then serialize the whole tree back to a string.
Building XML before saving
In real code you usually construct the tree dynamically with addChild() and serialize it at the end:
<?php
$catalog = new SimpleXMLElement('<catalog></catalog>');
$book = $catalog->addChild('book');
$book->addChild('title', 'Learn PHP');
$book->addChild('author', 'W3docs');
echo $catalog->saveXML();Output:
<?xml version="1.0"?>
<catalog><book><title>Learn PHP</title><author>W3docs</author></book></catalog>Saving XML to a file
Pass a filename to write the document straight to disk. In this mode saveXML() returns a boolean, so check it before assuming the write succeeded:
<?php
$xml = new SimpleXMLElement('<book><title>PHP Basics</title></book>');
$success = $xml->saveXML('output.xml');
if ($success) {
echo "XML saved successfully.";
} else {
echo "Failed to save XML.";
}A false result almost always means a permissions or path problem — the directory doesn't exist, or the PHP process can't write there.
Pretty-printing the output
SimpleXML serializes everything on a single line and offers no formatting flag. To get human-readable, indented XML, hand the string to DOMDocument, which does support formatOutput:
<?php
$xml = new SimpleXMLElement('<catalog><book><title>Learn PHP</title></book></catalog>');
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->saveXML());
echo $dom->saveXML();Output:
<?xml version="1.0"?>
<catalog>
<book>
<title>Learn PHP</title>
</book>
</catalog>Conclusion
SimpleXMLElement::saveXML() is the serialization step of the SimpleXML workflow: it converts a SimpleXMLElement into an XML string or writes it to a file using clean object-oriented syntax. Remember that it takes only an optional $filename, that it is an alias of asXML(), and that pretty-printing has to go through DOMDocument. For building documents before saving, see addChild(); for the broader API, see the SimpleXML overview.