PHP SimpleXMLElement::asXML() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::asXML() turns a SimpleXMLElement back into an XML string, or saves it to a file.

Call it on the root element to get the full document with the XML declaration. Call it on a child to get only that element. If you pass a file name, the XML is written to that file and the method returns true or false.

Syntax

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

PHP
$xml = $book->asXML();
CallWhat it does
$book->asXML()Returns the whole document as a string, with the XML declaration at the top.
$book->title->asXML()Returns only the title element as a string.
$book->asXML($file)Writes the document to a file and returns true on success.

The return type depends on the argument. Without a file name you get a string (or false on failure). With a file name you get true or false.

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

Parameters

ParameterDescription
filenameOptional. A file path to write the XML to. The default is null, which returns the XML as a string.

Return Value

Return ValueDescription
stringThe XML text, when no file name is given.
booltrue when the file was written, false on failure, when a file name is given.

PHP Examples

Example 1 — Print the Whole Document

Call asXML() on the root element to get the full XML text.

PHP
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');

echo $book->asXML();

How It Works

  1. simplexml_load_string() returns the root element.
  2. asXML() with no argument returns the XML as a string.
  3. The string starts with the XML declaration.
  4. The rest of the string is the book element and everything inside it.

Example 2 — A Child Has No XML Declaration

Call asXML() on a child element. You get only that element and no declaration.

PHP
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');

echo $book->title->asXML(), PHP_EOL;

$book->addChild('author', 'Ann');
echo $book->author->asXML(), PHP_EOL;

How It Works

  1. $book->title is a child element, not the document root.
  2. asXML() on a child returns only the title tag with its text.
  3. addChild() adds an author element to the book.
  4. asXML() on the new child returns just <author>Ann</author>.

Example 3 — Save the XML to a File

Pass a file name to write the XML to disk. A temporary file keeps the example safe to run.

PHP
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');

$file = tempnam(sys_get_temp_dir(), 'xml');
$saved = $book->asXML($file);

var_dump($saved);
echo file_get_contents($file);

unlink($file);

How It Works

  1. tempnam() creates an empty temporary file and returns its path.
  2. asXML($file) writes the document into that file and returns true.
  3. file_get_contents() reads the file back, so we can see the saved XML.
  4. unlink() removes the temporary file when we are done.

Frequently Asked Questions

It returns the XML of an element as a string. If you pass a file name, it saves the XML to that file instead.
It returns true when the file was written and false when it failed. Without a file name, it returns a string.
The declaration is added only when you call asXML() on the root element. Child elements return only their own tag.
They do the same job for SimpleXML objects. saveXML() is an alias of asXML().
No. SimpleXML writes the XML as it was loaded, without extra indentation. Use DOMDocument with formatOutput if you need nicely indented XML.

Next: SimpleXMLElement::attributes()

SimpleXMLElement::attributes() returns the attributes of an element, so you can loop over .

SimpleXMLElement::attributes() 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