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