Archive for March, 2007

Prototype: event observer

Event.observe('sp_btn_start', 'click', start_me, false);

Header XML

header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" ?>';

XML < = > ARRAY — class

<?php
 
class xml {
	var $name;
	var $encoding = null;
	var $nodes = array ();
	var $xml_string = null;
 
	function xml($name = 'xml_root', $encoding = 'UTF-8') {
		$this->encoding = $encoding;
		$this->name = $name;
	}
 
	function readFile($url) {
		$this->url = $url;
		$this->xml_string = file_get_contents($this->url);
	}
 
	function printOutXML($with_header = false) {
		if ($with_header) header("Content-Type: text/xml");
		echo $this->xml_string;
	}
 
	function array2xml($array) {
		$this->nodes = $array;
		$this->xml_string  = '<?xml version="1.0" encoding="'.$this->encoding.'" ?>';
		$this->xml_string .= "<" . $this->name . ">";
		foreach ($this->nodes AS $key => $node)
		$this->array2xml_callback($key, $node);
		$this->xml_string .= "</" . $this->name . ">";
		return $this->xml_string;
	}
 
	function array2xml_callback($key, $node) {
		$this->xml_string .= "<" . $key . ">";
		if (is_array($node)) {
			foreach ($node AS $child_key => $child_node)
			$this->array2xml_callback($child_key, $child_node);
		} else $this->xml_string .= $node."";
		$this->xml_string .= "</" . $key . ">";
	}
 
	function printOutARRAY($with_format = false) {
		if ($with_format) {
			echo '<pre/>';
			print_r($this->nodes);
			echo '<-pre>';
		}
		else print_r($this->nodes);
	}
 
	function xml2array ($xmldata = null) {
		if (is_null($xmldata)) $xmldata = $this->xml_string;
		$xmlreaderror = false;
		$parser = xml_parser_create ($this->encoding);
		xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
		xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
		if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
			$xmlreaderror = true;
			echo "error";
		}
		xml_parser_free ($parser);
		if (!$xmlreaderror) {
			$result = array ();
			$i = 0;
			if (isset ($vals [$i]['attributes']))
				foreach (array_keys ($vals [$i]['attributes']) as $attkey)
					$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
				if (!isset($attributes)) $attributes = array();
				$result [$vals [$i]['tag']] = array_merge ($attributes, $this->xml2array_callback ($vals, $i, 'open'));
		}
		$this->nodes = $result;
		return $result;
	}
 
	function xml2array_callback ($vals, &$i, $type) {
		if ($type == 'complete') {
			if (isset ($vals [$i]['value']))
			return ($vals [$i]['value']);
			else
			return '';
		}
		$children = array ();
		while ($vals [++$i]['type'] != 'close') {
			$type = $vals [$i]['type'];
			if (isset ($children [$vals [$i]['tag']])) {
				if (is_array ($children [$vals [$i]['tag']])) {
					$temp = array_keys ($children [$vals [$i]['tag']]);
					if (is_string ($temp [0])) {
						$a = $children [$vals [$i]['tag']];
						unset ($children [$vals [$i]['tag']]);
						$children [$vals [$i]['tag']][0] = $a;
					}
				} else {
					$a = $children [$vals [$i]['tag']];
					unset ($children [$vals [$i]['tag']]);
					$children [$vals [$i]['tag']][0] = $a;
				}
				$children [$vals [$i]['tag']][] = $this->xml2array_callback ($vals, $i, $type);
			} else
			$children [$vals [$i]['tag']] = $this->xml2array_callback ($vals, $i, $type);
			if (isset ($vals [$i]['attributes'])) {
				$attributes = array ();
				foreach (array_keys ($vals [$i]['attributes']) as $attkey)
				$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
				if (isset ($children [$vals [$i]['tag']])) {
					if ($children [$vals [$i]['tag']] == '') {
						unset ($children [$vals [$i]['tag']]);
						$children [$vals [$i]['tag']] = $attributes;
					}
					elseif (is_array ($children [$vals [$i]['tag']])) {
						$index = count ($children [$vals [$i]['tag']]) - 1;
						if ($children [$vals [$i]['tag']][$index] == '') {
							unset ($children [$vals [$i]['tag']][$index]);
							$children [$vals [$i]['tag']][$index] = $attributes;
						}
						$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
					} else {
						$value = $children [$vals [$i]['tag']];
						unset ($children [$vals [$i]['tag']]);
						$children [$vals [$i]['tag']]['value'] = $value;
						$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
					}
				} else
				$children [$vals [$i]['tag']] = $attributes;
			}
		}
		return $children;
	}
 
}
?>