3 * @brief This class contain functions to work with XML data
7 function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
10 foreach($array as $key => $value) {
11 foreach ($namespaces AS $nskey => $nsvalue)
12 $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
14 $root = new SimpleXMLElement("<".$key."/>");
15 self::from_array($value, $root, $remove_header, $namespaces, false);
17 $dom = dom_import_simplexml($root)->ownerDocument;
18 $dom->formatOutput = true;
21 $xml_text = $dom->saveXML();
24 $xml_text = trim(substr($xml_text, 21));
30 foreach($array as $key => $value) {
31 if ($key == "@attributes") {
32 if (!isset($element) OR !is_array($value))
35 foreach ($value as $attr_key => $attr_value) {
36 $element_parts = explode(":", $attr_key);
37 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
38 $namespace = $namespaces[$element_parts[0]];
42 $element->addAttribute ($attr_key, $attr_value, $namespace);
48 $element_parts = explode(":", $key);
49 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
50 $namespace = $namespaces[$element_parts[0]];
54 if (!is_array($value))
55 $element = $xml->addChild($key, xmlify($value), $namespace);
56 elseif (is_array($value)) {
57 $element = $xml->addChild($key, NULL, $namespace);
58 self::from_array($value, $element, $remove_header, $namespaces, false);
63 function copy(&$source, &$target, $elementname) {
64 if (count($source->children()) == 0)
65 $target->addChild($elementname, xmlify($source));
67 $child = $target->addChild($elementname);
68 foreach ($source->children() AS $childfield => $childentry)
69 self::copy($childentry, $child, $childfield);