X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fxml.php;h=76ad88cf48571002b60d9af868c565d6b31f857e;hb=08fb662b4a7b9c94a5882b0c6f47ed463dfb0ba2;hp=91480dc03b844d216eea6fea585f333c2d968f0e;hpb=f978bc9cc8011ab31e9abd90519008c6248cdebb;p=friendica.git diff --git a/include/xml.php b/include/xml.php index 91480dc03b..76ad88cf48 100644 --- a/include/xml.php +++ b/include/xml.php @@ -1,15 +1,34 @@ $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,21 +44,88 @@ 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); + } } } - function copy(&$source, &$target, $elementname) { + /** + * @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 + */ + public static 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) self::copy($childentry, $child, $childfield); } } + + /** + * @brief Create an XML element + * + * @param object $doc XML root + * @param string $element XML element name + * @param string $value XML value + * @param array $attributes array containing the attributes + * + * @return object XML element object + */ + public static function create_element($doc, $element, $value = "", $attributes = array()) { + $element = $doc->createElement($element, xmlify($value)); + + foreach ($attributes AS $key => $value) { + $attribute = $doc->createAttribute($key); + $attribute->value = xmlify($value); + $element->appendChild($attribute); + } + return $element; + } + + /** + * @brief Create an XML and append it to the parent object + * + * @param object $doc XML root + * @param object $parent parent object + * @param string $element XML element name + * @param string $value XML value + * @param array $attributes array containing the attributes + */ + public static function add_element($doc, $parent, $element, $value = "", $attributes = array()) { + $element = self::create_element($doc, $element, $value, $attributes); + $parent->appendChild($element); + } } ?>