PHP asXML() Function
Definition and Usage
The asXML() function returns the XML document from the SimpleXMLElement object as a string.
Returns false if the function 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' in the browser window, you will see the following HTML:
<note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>