PHP asXML() Function

Definition and Usage

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

If it fails, it returns false.

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>Do not 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 Do not forget the meeting!

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

<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Do not forget the meeting!</body>
</note>