SimpleXml to string

liysd picture liysd · Sep 11, 2010 · Viewed 141.4k times · Source

Is there any function that makes string from PHP SimpleXMLElement?

Answer

Tim Cooper picture Tim Cooper · Sep 11, 2010

You can use the SimpleXMLElement::asXML() method to accomplish this:

$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);

// The entire XML tree as a string:
// "<element><child>Hello World</child></element>"
$xml->asXML();

// Just the child node as a string:
// "<child>Hello World</child>"
$xml->child->asXML();