Forcing a SimpleXML Object to a string, regardless of context Forcing a SimpleXML Object to a string, regardless of context xml xml

Forcing a SimpleXML Object to a string, regardless of context


Typecast the SimpleXMLObject to a string:

$foo = array( (string) $xml->channel->item->title );

The above code internally calls __toString() on the SimpleXMLObject. This method is not publicly available, as it interferes with the mapping scheme of the SimpleXMLObject, but it can still be invoked in the above manner.


You can use the PHP function

strval();

This function returns the string values of the parameter passed to it.


There is native SimpleXML method SimpleXMLElement::asXMLDepending on parameter it writes SimpleXMLElement to xml 1.0 file or just to a string:

$xml = new SimpleXMLElement($string);$validfilename = '/temp/mylist.xml';$xml->asXML($validfilename);    // to a fileecho $xml->asXML();             // to a string