' . PHP_EOL;
return($templates_xml);
}
/**
* Convert an multi-dimensional array to xml.
* http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml
*
* @param object $object Link to SimpleXMLElement object
* @param array $data Array which need to convert into xml
*/
function array_to_xml(array $data, SimpleXMLElement $object)
{
foreach ($data as $key => $value)
{
if (is_array($value))
{
if (is_array_assoc($value))
{
// For associative arrays use keys as child object
$new_object = $object->addChild($key);
array_to_xml($value, $new_object);
} else {
// For sequential arrays use parent key as child
foreach ($value as $new_value)
{
if (is_array($new_value))
{
array_to_xml(array($key => $new_value), $object);
} else {
$object->addChild($key, $new_value);
}
}
}
} else {
//$object->$key = $value; // See note about & here - http://php.net/manual/en/simplexmlelement.addchild.php#112204
$object->addChild($key, $value);
}
}
}
function xml_to_array($xml_string)
{
$xml = simplexml_load_string($xml_string);
// r($xml);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
return $array;
}
/**
* Pretty print for xml string
*
* @param string $xml An xml string
* @param boolean $formatted Convert or not output to human formatted xml
*/
function print_xml($xml, $formatted = TRUE)
{
if ($formatted)
{
$xml = format_xml($xml);
}
if (is_cli())
{
echo $xml;
} else {
echo generate_box_open(array('title' => 'Output', 'padding' => TRUE));
echo '
' . escape_html($xml) . '
NOTE: XML values are always escaped, that\'s why you can see this ' . escape_html(escape_html('< > & " \'')) .
' instead of this ' . escape_html('< > & " \'') . '. Leave them as is.
' . PHP_EOL;
echo generate_box_close();
}
}
/**
* Convert unformatted XML string to human readable string
*
* @param string $xml Unformatted XML string
* @return string Human formatted XML string
*/
function format_xml($xml)
{
if (!class_exists('DOMDocument'))
{
// If not exist class, just return original string
return $xml;
}
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
$dom->loadXML($xml);
return $dom->saveXML();
}
/**
* Send any string to browser as file
*
* @param string $string String content for save as file
* @param string $filename Filename
* @param array $vars Vars with some options
*/
function download_as_file($string, $filename = "observium_export.xml", $vars = array())
{
//$echo = ob_get_contents();
ob_end_clean(); // Clean and disable buffer
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext == 'xml')
{
header('Content-type: text/xml');
if ($vars['formatted'] == 'yes')
{
$string = format_xml($string);
}
} else {
header('Content-type: text/plain');
}
header('Content-Disposition: attachment; filename="'.$filename.'";');
header("Content-Length: " . strlen($string));
header("Pragma: no-cache");
header("Expires: 0");
echo($string); // Send string content to browser output
exit(0); // Stop any other output
}
// EOF