]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-xml.php
Removed closing PHP tag on include files
[Github/YOURLS.git] / includes / functions-xml.php
1 <?php\r
2 /*************************************************************************/\r
3 /* This class stores associative arrays in an xml formated string.       */\r
4 /* There's also a function thar retrieves them. If you try to use        */ \r
5 /* xml2array with a general xml, it can fail, since there can be some    */\r
6 /* repeated indexes....                                                  */\r
7 /* Source: http://www.phpclasses.org/browse/package/2286/                */\r
8 /*************************************************************************/\r
9 \r
10 class yourls_array2xml {\r
11         var $text;\r
12         var $arrays, $keys, $node_flag, $depth, $xml_parser;\r
13         /*Converts an array to an xml string*/\r
14         function array2xml($array) {\r
15         //global $text;\r
16         $this->text="<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><result>";\r
17         $this->text.= $this->array_transform($array);\r
18         $this->text .="</result>";\r
19         return $this->text;\r
20         }\r
21 \r
22         function array_transform($array){\r
23         //global $array_text;\r
24         foreach($array as $key => $value){\r
25         if(!is_array($value)){\r
26                //BEGIN code mod by Doug Vanderweide, 13 Jan 2011\r
27                //does $value contain html entities?\r
28                if(strlen($value) != strlen(htmlentities($value))) {\r
29                        //if so, encode as CDATA\r
30                        $value = "<![CDATA[" . htmlentities($value) . "]]>";\r
31                }\r
32                $this->text .= "<$key>$value</$key>";\r
33                //END code mod\r
34          } else {\r
35          $this->text.="<$key>";\r
36          $this->array_transform($value);\r
37          $this->text.="</$key>";\r
38          }\r
39         }\r
40         //return $array_text;\r
41 \r
42         }\r
43         /*Transform an XML string to associative array "XML Parser Functions"*/\r
44         function xml2array($xml){\r
45         $this->depth=-1;\r
46         $this->xml_parser = xml_parser_create();\r
47         xml_set_object($this->xml_parser, $this);\r
48         xml_parser_set_option ($this->xml_parser,XML_OPTION_CASE_FOLDING,0);//Don't put tags uppercase\r
49         xml_set_element_handler($this->xml_parser, "startElement", "endElement");\r
50         xml_set_character_data_handler($this->xml_parser,"characterData");\r
51         xml_parse($this->xml_parser,$xml,true);\r
52         xml_parser_free($this->xml_parser);\r
53         return $this->arrays[0];\r
54 \r
55         }\r
56         function startElement($parser, $name, $attrs)\r
57          {\r
58            $this->keys[]=$name; //We add a key\r
59            $this->node_flag=1;\r
60            $this->depth++;\r
61          }\r
62         function characterData($parser,$data)\r
63          {\r
64            $key=end($this->keys);\r
65            $this->arrays[$this->depth][$key]=$data;\r
66            $this->node_flag=0; //So that we don't add as an array, but as an element\r
67          }\r
68         function endElement($parser, $name)\r
69          {\r
70            $key=array_pop($this->keys);\r
71            //If $node_flag==1 we add as an array, if not, as an element\r
72            if($this->node_flag==1){\r
73              $this->arrays[$this->depth][$key]=$this->arrays[$this->depth+1];\r
74              unset($this->arrays[$this->depth+1]);\r
75            }\r
76            $this->node_flag=1;\r
77            $this->depth--;\r
78          }\r
79 \r
80 }//End of the class\r
81 \r