3 * @file include/xml.php
8 * @brief This class contain functions to work with XML data
13 * @brief Creates an XML structure out of a given array
15 * @param array $array The array of the XML structure that will be generated
16 * @param object $xml The createdXML will be returned by reference
17 * @param bool $remove_header Should the XML header be removed or not?
18 * @param array $namespaces List of namespaces
19 * @param bool $root - interally used parameter. Mustn't be used from outside.
21 * @return string The created XML
23 public static function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
26 foreach($array as $key => $value) {
27 foreach ($namespaces AS $nskey => $nsvalue)
28 $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
30 $root = new SimpleXMLElement("<".$key."/>");
31 self::from_array($value, $root, $remove_header, $namespaces, false);
33 $dom = dom_import_simplexml($root)->ownerDocument;
34 $dom->formatOutput = true;
37 $xml_text = $dom->saveXML();
40 $xml_text = trim(substr($xml_text, 21));
46 foreach($array as $key => $value) {
47 if ($key == "@attributes") {
48 if (!isset($element) OR !is_array($value))
51 foreach ($value as $attr_key => $attr_value) {
52 $element_parts = explode(":", $attr_key);
53 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
54 $namespace = $namespaces[$element_parts[0]];
58 $element->addAttribute ($attr_key, $attr_value, $namespace);
64 $element_parts = explode(":", $key);
65 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
66 $namespace = $namespaces[$element_parts[0]];
70 if (!is_array($value))
71 $element = $xml->addChild($key, xmlify($value), $namespace);
72 elseif (is_array($value)) {
73 $element = $xml->addChild($key, NULL, $namespace);
74 self::from_array($value, $element, $remove_header, $namespaces, false);
80 * @brief Copies an XML object
82 * @param object $source The XML source
83 * @param object $target The XML target
84 * @param string $elementname Name of the XML element of the target
86 public static function copy(&$source, &$target, $elementname) {
87 if (count($source->children()) == 0)
88 $target->addChild($elementname, xmlify($source));
90 $child = $target->addChild($elementname);
91 foreach ($source->children() AS $childfield => $childentry)
92 self::copy($childentry, $child, $childfield);