PHP SimpleXMLElement::saveXML() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::saveXML() turns a SimpleXML object back into an XML string, or writes that XML to a file when you pass a file name.

It is an alias of SimpleXMLElement::asXML(), so both methods do exactly the same thing. When you call it on the root element, the result starts with the XML declaration. When you call it on a child element, you get only that element. In modern code, prefer asXML() because it is the main name in the documentation.

Syntax

Call SimpleXMLElement::saveXML() with the values you need. Start with this line.

PHP
$xml->saveXML();
CallWhat it does
$xml->saveXML()Returns the whole document as an XML string, including the XML declaration.
$xml->title->saveXML()Returns only the title element as a string, without a declaration.
$xml->saveXML('book.xml')Writes the XML to book.xml and returns true when the file was saved.

saveXML() is an alias of asXML(). The two names give the same result, so pick asXML() in new code and use saveXML() only when you read older code. Any change you made through SimpleXML, such as a new value or a new child, is included in the output.

Library declarations
PHP
SimpleXMLElement::saveXML(?string $filename = null): string|bool

Parameters

ParameterDescription
filenameOptional. A file name or path. When you give it, PHP writes the XML to that file instead of returning it. When you leave it out or pass null, the XML is returned as a string.

Return Value

Return ValueDescription
stringThe XML text when no file name is given. It returns false if the XML could not be produced.
booltrue when a file name is given and the file is written, or false if writing fails.

PHP Examples

Example 1 — Get the XML as a String

Load a tiny XML string and turn it back into text with saveXML().

PHP
<?php
$xml = simplexml_load_string('<book><title>PHP</title></book>');

echo $xml->saveXML();
echo 'Type: ', gettype($xml->saveXML()), PHP_EOL;

How It Works

  1. simplexml_load_string() turns the XML text into a SimpleXMLElement object.
  2. saveXML() on the root element returns the XML declaration, the whole book element, and a final newline.
  3. echo prints that string as it is, so the output has two lines.
  4. gettype() confirms that saveXML() returned a string when no file name was passed.

Example 2 — Changes, Child Elements, and the asXML() Alias

Edit the object first, then save it. Also compare saveXML() on the root with saveXML() on one child, and with asXML().

PHP
<?php
$xml = simplexml_load_string('<book><title>PHP</title></book>');

$xml->title = 'Learn PHP';
$xml->addChild('year', '2024');

echo $xml->saveXML();
echo $xml->title->saveXML(), PHP_EOL;
echo 'Same as asXML: ', var_export($xml->saveXML() === $xml->asXML(), true), PHP_EOL;

How It Works

  1. Assigning to $xml->title changes the text of the title element, and addChild() adds a year element.
  2. saveXML() on the root includes both changes, plus the XML declaration.
  3. saveXML() on $xml->title returns only the title element, with no declaration.
  4. The last line shows that saveXML() and asXML() return the same string, because one is an alias of the other.

Example 3 — Save the XML to a File

Pass a file name to write the XML to disk. This example uses a temporary file and removes it at the end, so the output stays the same on every run.

PHP
<?php
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun-save-xml.xml';

$xml = simplexml_load_string('<book><title>PHP</title></book>');
$saved = $xml->saveXML($file);

echo 'Saved: ', var_export($saved, true), PHP_EOL;
echo trim(file_get_contents($file)), PHP_EOL;

unlink($file);
echo 'Deleted: ', var_export(!file_exists($file), true), PHP_EOL;

How It Works

  1. The file path is built inside the system temp folder, so it works on any computer.
  2. saveXML($file) writes the XML to that file and returns true instead of returning a string.
  3. file_get_contents() reads the file back, and trim() removes the final newline before printing.
  4. unlink() deletes the temporary file, and file_exists() confirms that it is gone.

Frequently Asked Questions

It converts a SimpleXMLElement back into XML text. If you pass a file name, it writes the XML to that file and returns true instead.
Yes. saveXML() is an alias of asXML(), so both methods accept the same argument and return the same result. Use asXML() in new code.
When you call saveXML() on the root element, PHP adds the declaration at the top. If you call it on a child element, the declaration is not added.
Not directly. saveXML() writes the XML on one line. To get indented output, import the object into DOMDocument with dom_import_simplexml(), set formatOutput to true, and call saveXML() on the DOM object.
It returns true when the file is written, and false when it fails, for example because the folder does not exist or is not writable.

Next: simplexml_import_dom()

simplexml_import_dom() takes a DOMDocument or DOMElement and gives you a SimpleXMLElement .

simplexml_import_dom() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful