PHP asXML() Function

Definition and Usage

The asXML() function returns the XML document as a string from the SimpleXMLElement object.

Returns false if it fails.

Syntax

class SimpleXMLElement
{
string asXML(file)
}
Parameter Description
file Optional. If this parameter is specified, the function will write the XML to a file instead of returning it.

Example

XML File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

PHP Code:

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  }
echo $xml->asXML();
?>

Output:

George John Reminder Don't forget the meeting!

If you select 'View Source File' in the browser window, you will see this HTML:

<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>