]> git.mxchange.org Git - friendica.git/blob - include/xml.php
Some more code cleaning
[friendica.git] / include / xml.php
1 <?php
2 /**
3  * @file include/xml.php
4  * @brief This class contain functions to work with XML data
5  *
6  */
7 class xml {
8         /**
9          * @brief Creates an XML structure out of a given array
10          *
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
15          *
16          * @return string The created XML
17          */
18         function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
19
20                 if ($root) {
21                         foreach($array as $key => $value) {
22                                 foreach ($namespaces AS $nskey => $nsvalue)
23                                         $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
24
25                                 $root = new SimpleXMLElement("<".$key."/>");
26                                 self::from_array($value, $root, $remove_header, $namespaces, false);
27
28                                 $dom = dom_import_simplexml($root)->ownerDocument;
29                                 $dom->formatOutput = true;
30                                 $xml = $dom;
31
32                                 $xml_text = $dom->saveXML();
33
34                                 if ($remove_header)
35                                         $xml_text = trim(substr($xml_text, 21));
36
37                                 return $xml_text;
38                         }
39                 }
40
41                 foreach($array as $key => $value) {
42                         if ($key == "@attributes") {
43                                 if (!isset($element) OR !is_array($value))
44                                         continue;
45
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]];
50                                         else
51                                                 $namespace = NULL;
52
53                                         $element->addAttribute ($attr_key, $attr_value, $namespace);
54                                 }
55
56                                 continue;
57                         }
58
59                         $element_parts = explode(":", $key);
60                         if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
61                                 $namespace = $namespaces[$element_parts[0]];
62                         else
63                                 $namespace = NULL;
64
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);
70                         }
71                 }
72         }
73
74         /**
75          * @brief Copies an XML object
76          *
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
80          */
81         function copy(&$source, &$target, $elementname) {
82                 if (count($source->children()) == 0)
83                         $target->addChild($elementname, xmlify($source));
84                 else {
85                         $child = $target->addChild($elementname);
86                         foreach ($source->children() AS $childfield => $childentry)
87                                 self::copy($childentry, $child, $childfield);
88                 }
89         }
90 }
91 ?>