Example 1 — Print the Whole Document
Call asXML() on the root element to get the full XML text.
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');
echo $book->asXML(); How It Works
- simplexml_load_string() returns the root element.
- asXML() with no argument returns the XML as a string.
- The string starts with the XML declaration.
- The rest of the string is the book element and everything inside it.

