]> git.mxchange.org Git - friendica.git/blobdiff - include/xml.php
Removed moved function
[friendica.git] / include / xml.php
index 9c458dab12da0a4e1d0ab158a23cee6924479dda..c74c23c473ce3254a717e155e8c6a707958dd58e 100644 (file)
@@ -4,31 +4,65 @@
  *
  */
 class xml {
-       function from_array($array, &$xml) {
+       function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
 
-               if (!is_object($xml)) {
+               if ($root) {
                        foreach($array as $key => $value) {
+                               foreach ($namespaces AS $nskey => $nsvalue)
+                                       $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
+
                                $root = new SimpleXMLElement("<".$key."/>");
-                               self::from_array($value, $root);
+                               self::from_array($value, $root, $remove_header, $namespaces, false);
 
                                $dom = dom_import_simplexml($root)->ownerDocument;
                                $dom->formatOutput = true;
                                $xml = $dom;
-                               return $dom->saveXML();
+
+                               $xml_text = $dom->saveXML();
+
+                               if ($remove_header)
+                                       $xml_text = trim(substr($xml_text, 21));
+
+                               return $xml_text;
                        }
                }
 
                foreach($array as $key => $value) {
-                       if (!is_array($value) AND !is_numeric($key))
-                               $xml->addChild($key, xmlify($value));
-                       elseif (is_array($value))
-                               self::from_array($value, $xml->addChild($key));
+                       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);
+                       }
                }
        }
 
        function copy(&$source, &$target, $elementname) {
                if (count($source->children()) == 0)
-                       $target->addChild($elementname, $source);
+                       $target->addChild($elementname, xmlify($source));
                else {
                        $child = $target->addChild($elementname);
                        foreach ($source->children() AS $childfield => $childentry)