Kolejna klasa, która wpadła w moje ręce, kiedy poszukiwałem rozwiązania do zapisywania danych do pliku XML. Oczywiście sprawa byłaby prostsza, gdyby serwer obslugiwał SimpleXML, ale tak nie było. Dlatego potrzebne było inne rozwiązanie.
|
<br/><?php<br/><br/>function & XML_unserialize(&$xml){<br/> $xml_parser = &new XML();<br/> $data = &$xml_parser->parse($xml);<br/> $xml_parser->destruct();<br/> return $data;<br/>}<br/><br/>function & XML_serialize(&$data, $level = 0, $prior_key = NULL){<br/> if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"n"; }<br/> while(list($key, $value) = each($data))<br/> if(!strpos($key, ' attr')) #if it's not an attribute<br/> #we don't treat attributes by themselves, so for an empty element<br/> # that has attributes you still need to set the element to NULL<br/><br/> if(is_array($value) and array_key_exists(0, $value)){<br/> XML_serialize($value, $level, $key);<br/> }else{<br/> $tag = $prior_key ? $prior_key : $key;<br/> echo str_repeat("t", $level),'<',$tag;<br/> if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element<br/> while(list($attr_name, $attr_value) = each($data["$key attr"]))<br/> echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';<br/> reset($data["$key attr"]);<br/> }<br/><br/> if(is_null($value)) echo " />n";<br/> elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>n";<br/> else echo ">n",XML_serialize($value, $level+1),str_repeat("t", $level),"</$tag>n";<br/> }<br/> reset($data);<br/> if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }<br/>}<br/><br/>class XML{<br/> var $parser; #a reference to the XML parser<br/> var $document; #the entire XML structure built up so far<br/> var $parent; #a pointer to the current parent - the parent will be an array<br/> var $stack; #a stack of the most recent parent at each nesting level<br/> var $last_opened_tag; #keeps track of the last tag opened.<br/><br/> function XML(){<br/> $this->parser = &xml_parser_create();<br/> xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);<br/> xml_set_object(&$this->parser, &$this);<br/> xml_set_element_handler(&$this->parser, 'open','close');<br/> xml_set_character_data_handler(&$this->parser, 'data');<br/> }<br/> function destruct(){ xml_parser_free(&$this->parser); }<br/> function & parse(&$data){<br/> $this->document = array();<br/> $this->stack = array();<br/> $this->parent = &$this->document;<br/> return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;<br/> }<br/> function open(&$parser, $tag, $attributes){<br/> $this->data = ''; #stores temporary cdata<br/> $this->last_opened_tag = $tag;<br/> if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before<br/> if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric<br/> #this is the third or later instance of $tag we've come across<br/> $key = count_numeric_items($this->parent[$tag]);<br/> }else{<br/> #this is the second instance of $tag that we've seen. shift around<br/> if(array_key_exists("$tag attr",$this->parent)){<br/> $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);<br/> unset($this->parent["$tag attr"]);<br/> }else{<br/> $arr = array(&$this->parent[$tag]);<br/> }<br/> $this->parent[$tag] = &$arr;<br/> $key = 1;<br/> }<br/> $this->parent = &$this->parent[$tag];<br/> }else{<br/> $key = $tag;<br/> }<br/> if($attributes) $this->parent["$key attr"] = $attributes;<br/> $this->parent = &$this->parent[$key];<br/> $this->stack[] = &$this->parent;<br/> }<br/> function data(&$parser, $data){<br/> if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags<br/> $this->data .= $data;<br/> }<br/> function close(&$parser, $tag){<br/> if($this->last_opened_tag == $tag){<br/> $this->parent = $this->data;<br/> $this->last_opened_tag = NULL;<br/> }<br/> array_pop($this->stack);<br/> if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];<br/> }<br/>}<br/>function count_numeric_items(&$array){<br/> return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;<br/>}<br/>?> |