2016-03-05 18:53:30 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2016-03-23 17:12:08 -04:00
|
|
|
* @file include/xml.php
|
2016-03-29 11:54:36 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-05 18:53:30 -05:00
|
|
|
* @brief This class contain functions to work with XML data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class xml {
|
2016-03-20 11:16:15 -04:00
|
|
|
/**
|
|
|
|
* @brief Creates an XML structure out of a given array
|
|
|
|
*
|
|
|
|
* @param array $array The array of the XML structure that will be generated
|
|
|
|
* @param object $xml The createdXML will be returned by reference
|
|
|
|
* @param bool $remove_header Should the XML header be removed or not?
|
|
|
|
* @param array $namespaces List of namespaces
|
2016-03-27 18:06:46 -04:00
|
|
|
* @param bool $root - interally used parameter. Mustn't be used from outside.
|
2016-03-20 11:16:15 -04:00
|
|
|
*
|
|
|
|
* @return string The created XML
|
|
|
|
*/
|
2016-03-27 17:25:32 -04:00
|
|
|
public static function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
|
2016-03-05 18:53:30 -05:00
|
|
|
|
2016-03-13 04:57:44 -04:00
|
|
|
if ($root) {
|
2016-03-05 18:53:30 -05:00
|
|
|
foreach($array as $key => $value) {
|
2016-03-13 04:57:44 -04:00
|
|
|
foreach ($namespaces AS $nskey => $nsvalue)
|
|
|
|
$key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
|
|
|
|
|
2016-03-05 18:53:30 -05:00
|
|
|
$root = new SimpleXMLElement("<".$key."/>");
|
2016-03-13 04:57:44 -04:00
|
|
|
self::from_array($value, $root, $remove_header, $namespaces, false);
|
2016-03-05 18:53:30 -05:00
|
|
|
|
|
|
|
$dom = dom_import_simplexml($root)->ownerDocument;
|
|
|
|
$dom->formatOutput = true;
|
|
|
|
$xml = $dom;
|
2016-03-13 01:10:24 -05:00
|
|
|
|
|
|
|
$xml_text = $dom->saveXML();
|
|
|
|
|
|
|
|
if ($remove_header)
|
|
|
|
$xml_text = trim(substr($xml_text, 21));
|
|
|
|
|
|
|
|
return $xml_text;
|
2016-03-05 18:53:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($array as $key => $value) {
|
2016-03-13 04:57:44 -04:00
|
|
|
if ($key == "@attributes") {
|
|
|
|
if (!isset($element) OR !is_array($value))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foreach ($value as $attr_key => $attr_value) {
|
|
|
|
$element_parts = explode(":", $attr_key);
|
|
|
|
if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
|
|
|
|
$namespace = $namespaces[$element_parts[0]];
|
|
|
|
else
|
|
|
|
$namespace = NULL;
|
|
|
|
|
|
|
|
$element->addAttribute ($attr_key, $attr_value, $namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$element_parts = explode(":", $key);
|
|
|
|
if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
|
|
|
|
$namespace = $namespaces[$element_parts[0]];
|
|
|
|
else
|
|
|
|
$namespace = NULL;
|
|
|
|
|
|
|
|
if (!is_array($value))
|
|
|
|
$element = $xml->addChild($key, xmlify($value), $namespace);
|
|
|
|
elseif (is_array($value)) {
|
|
|
|
$element = $xml->addChild($key, NULL, $namespace);
|
|
|
|
self::from_array($value, $element, $remove_header, $namespaces, false);
|
|
|
|
}
|
2016-03-05 18:53:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 11:16:15 -04:00
|
|
|
/**
|
|
|
|
* @brief Copies an XML object
|
|
|
|
*
|
|
|
|
* @param object $source The XML source
|
|
|
|
* @param object $target The XML target
|
|
|
|
* @param string $elementname Name of the XML element of the target
|
|
|
|
*/
|
2016-03-27 17:25:32 -04:00
|
|
|
public static function copy(&$source, &$target, $elementname) {
|
2016-03-05 18:53:30 -05:00
|
|
|
if (count($source->children()) == 0)
|
2016-03-14 18:54:01 -04:00
|
|
|
$target->addChild($elementname, xmlify($source));
|
2016-03-05 18:53:30 -05:00
|
|
|
else {
|
|
|
|
$child = $target->addChild($elementname);
|
|
|
|
foreach ($source->children() AS $childfield => $childentry)
|
|
|
|
self::copy($childentry, $child, $childfield);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|