3 * @file include/xml.php
4 * @brief This class contain functions to work with XML data
9 * @brief Creates an XML structure out of a given array
11 * @param array $array The array of the XML structure that will be generated
12 * @param object $xml The createdXML will be returned by reference
13 * @param bool $remove_header Should the XML header be removed or not?
14 * @param array $namespaces List of namespaces
16 * @return string The created XML
18 function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
21 foreach($array as $key => $value) {
22 foreach ($namespaces AS $nskey => $nsvalue)
23 $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
25 $root = new SimpleXMLElement("<".$key."/>");
26 self::from_array($value, $root, $remove_header, $namespaces, false);
28 $dom = dom_import_simplexml($root)->ownerDocument;
29 $dom->formatOutput = true;
32 $xml_text = $dom->saveXML();
35 $xml_text = trim(substr($xml_text, 21));
41 foreach($array as $key => $value) {
42 if ($key == "@attributes") {
43 if (!isset($element) OR !is_array($value))
46 foreach ($value as $attr_key => $attr_value) {
47 $element_parts = explode(":", $attr_key);
48 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
49 $namespace = $namespaces[$element_parts[0]];
53 $element->addAttribute ($attr_key, $attr_value, $namespace);
59 $element_parts = explode(":", $key);
60 if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
61 $namespace = $namespaces[$element_parts[0]];
65 if (!is_array($value))
66 $element = $xml->addChild($key, xmlify($value), $namespace);
67 elseif (is_array($value)) {
68 $element = $xml->addChild($key, NULL, $namespace);
69 self::from_array($value, $element, $remove_header, $namespaces, false);
75 * @brief Copies an XML object
77 * @param object $source The XML source
78 * @param object $target The XML target
79 * @param string $elementname Name of the XML element of the target
81 function copy(&$source, &$target, $elementname) {
82 if (count($source->children()) == 0)
83 $target->addChild($elementname, xmlify($source));
85 $child = $target->addChild($elementname);
86 foreach ($source->children() AS $childfield => $childentry)
87 self::copy($childentry, $child, $childfield);