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