/**
* Format XML, ie indent it for pretty printing
*
* @param $xml simplexml object
* @return string
**/
static function formatXML($xml)
{
$string = str_replace("><", ">\n<", $xml->asXML());
$token = strtok($string, "\n");
$result = '';
$pad = 0;
$matches = array();
$indent = 0;
while ($token !== FALSE) {
// 1. open and closing tags on same line - no change
if (preg_match('/.+<\\/\\w[^>]*>$/', $token, $matches)) {
$indent = 0;
// 2. closing tag - outdent now
} else {
if (preg_match('/^<\\/\\w/', $token, $matches)) {
$pad = $pad - 3;
// 3. opening tag - don't pad this one, only subsequent tags
} else {
if (preg_match('/^<\\w[^>]*[^\\/]>.*$/', $token, $matches)) {
$indent = 3;
} else {
$indent = 0;
}
}
}
$line = Toolbox::str_pad($token, strlen($token) + $pad, ' ', STR_PAD_LEFT);
$result .= $line . "\n";
$token = strtok("\n");
$pad += $indent;
$indent = 0;
}
return $result;
}