SimpleXml to string
To convert a SimpleXml object to a string in PHP, you can use the asXML()
function. For example:
<?php
$xml = new SimpleXmlElement('<example>Hello World</example>');
$string = $xml->asXML();
echo $string;
This will convert the $xml
object to a string with the contents <example>Hello World</example>
.
Alternatively, you can also use the saveXML()
function, which accepts an optional parameter for the node to return as a string.
<?php
$xml = new SimpleXmlElement('<example>Hello World</example>');
$string = $xml->saveXML();
echo $string;
Both asXML()
and saveXML()
will convert the entire SimpleXml object to a string.