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
10 class yourls_array2xml {
\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
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
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 $this->text .= "<$key>$value</$key>";
\r
28 $this->text.="<$key>";
\r
29 $this->array_transform($value);
\r
30 $this->text.="</$key>";
\r
33 //return $array_text;
\r
36 /*Transform an XML string to associative array "XML Parser Functions"*/
\r
37 function xml2array($xml){
\r
39 $this->xml_parser = xml_parser_create();
\r
40 xml_set_object($this->xml_parser, $this);
\r
41 xml_parser_set_option ($this->xml_parser,XML_OPTION_CASE_FOLDING,0);//Don't put tags uppercase
\r
42 xml_set_element_handler($this->xml_parser, "startElement", "endElement");
\r
43 xml_set_character_data_handler($this->xml_parser,"characterData");
\r
44 xml_parse($this->xml_parser,$xml,true);
\r
45 xml_parser_free($this->xml_parser);
\r
46 return $this->arrays[0];
\r
49 function startElement($parser, $name, $attrs)
\r
51 $this->keys[]=$name; //We add a key
\r
55 function characterData($parser,$data)
\r
57 $key=end($this->keys);
\r
58 $this->arrays[$this->depth][$key]=$data;
\r
59 $this->node_flag=0; //So that we don't add as an array, but as an element
\r
61 function endElement($parser, $name)
\r
63 $key=array_pop($this->keys);
\r
64 //If $node_flag==1 we add as an array, if not, as an element
\r
65 if($this->node_flag==1){
\r
66 $this->arrays[$this->depth][$key]=$this->arrays[$this->depth+1];
\r
67 unset($this->arrays[$this->depth+1]);
\r