X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fxml.php;h=2aed3fe8edcacfc54fc7ff73589c4d5cb496a88c;hb=206c26ae30e711aa30cdb29d668bee9dce834c28;hp=91480dc03b844d216eea6fea585f333c2d968f0e;hpb=f978bc9cc8011ab31e9abd90519008c6248cdebb;p=friendica.git diff --git a/include/xml.php b/include/xml.php index 91480dc03b..2aed3fe8ed 100644 --- a/include/xml.php +++ b/include/xml.php @@ -4,12 +4,25 @@ * */ class xml { - function from_array($array, &$xml, $remove_header = false) { + /** + * @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 + * + * @return string The created 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; @@ -25,16 +38,48 @@ class xml { } 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); + } } } + /** + * @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 + */ 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)