3 * @brief This class contain functions to work with XML data
8 * @brief Creates an XML structure out of a given array
10 * @param array $array The array of the XML structure that will be generated
11 * @param object $xml The createdXML will be returned by reference
12 * @param bool $remove_header Should the XML header be removed or not?
13 * @param array $namespaces List of namespaces
15 * @return string The created XML
17 function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
20 foreach($array as $key => $value) {
21 foreach ($namespaces AS $nskey => $nsvalue)
22 $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
24 $root = new SimpleXMLElement("<".$key."/>");
25 self::from_array($value, $root, $remove_header, $namespaces, false);
27 $dom = dom_import_simplexml($root)->ownerDocument;
28 $dom->formatOutput = true;
31 $xml_text = $dom->saveXML();
34 $xml_text = trim(substr($xml_text, 21));
40 foreach($array as $key => $value) {
41 if ($key == "@attributes") {
42 if (!isset($element) OR !is_array($value))
45 foreach ($value as $attr_key => $attr_value) {
46 $element_parts = explode(":", $attr_key);
47 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
48 $namespace = $namespaces[$element_parts[0]];
52 $element->addAttribute ($attr_key, $attr_value, $namespace);
58 $element_parts = explode(":", $key);
59 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
60 $namespace = $namespaces[$element_parts[0]];
64 if (!is_array($value))
65 $element = $xml->addChild($key, xmlify($value), $namespace);
66 elseif (is_array($value)) {
67 $element = $xml->addChild($key, NULL, $namespace);
68 self::from_array($value, $element, $remove_header, $namespaces, false);
74 * @brief Copies an XML object
76 * @param object $source The XML source
77 * @param object $target The XML target
78 * @param string $elementname Name of the XML element of the target
80 function copy(&$source, &$target, $elementname) {
81 if (count($source->children()) == 0)
82 $target->addChild($elementname, xmlify($source));
84 $child = $target->addChild($elementname);
85 foreach ($source->children() AS $childfield => $childentry)
86 self::copy($childentry, $child, $childfield);