Location: PHPKode > scripts > phpCookBook > phpcookbook/includes/class.xml.php
<?php
/*
phpCookBook XML Parser
http://phpcookbook.sourceforge.net
Copyright (c)2002 Floris Barthel <hide@address.com>

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
included LICENCE file for more details.
*/
class XMLParse
{
	var $parser;
	var $elements = array();
	var $tag;
	var $output;

	var $supplies;
	var $ingredients;
	var $ing_loop;
	var $sup_loop;

	function XMLParse($encoding = "ISO-8859-1")
	{
		// Create the parser
		$this->parser = xml_parser_create($encoding);
		unset($encoding);
		//Set handlers
		xml_set_object($this->parser, &$this);
		xml_set_character_data_handler($this->parser, "_CDATA");
		xml_set_element_handler($this->parser, "_StartElement", "_EndElement");
	}

	function Parse($data, $last = '')
	{
		if(!isset($last))
			return xml_parse($this->parser, $data);
		else
			return xml_parse($this->parser, $data, $last);
	}

	function SetTagData($tag, $value)
	{
		$this->tag[$tag] = $value;
	}

	function SetElementData($data)
	{
		$this->elements[0]['data'] .= $data;
	}

	function setCount($ings, $supls)
	{
		$this->ingredients = $ings;
		$this->supplies = $supls;
	}

	function SetOutput($value)
	{
		$this->output = $value;
	}

	function _CDATA($parser, $data)
	{
		$this->SetElementData($data);
	}

	function _StartElement($parser, $tag, $attributes)
	{
		$this->SetTagData('attributes', $attributes);
		$this->SetTagData('data', '');
		$this->SetTagData('tag', strtolower($tag));
		array_unshift($this->elements, $this->tag);
	}

	function _EndElement($parser, $tag)
	{
		static $count = 0;
		$tag = strtolower($tag);
		if($tag == $this->elements[0]['tag']) {
			if(isset($this->elements[0]['attributes'])) {
				$attributes = $this->elements[0]['attributes'];
			}
			$data = $this->elements[0]['data'];
			array_shift($this->elements);
			if($count == 0) {
				$lines = file('templates/main-template.htm');
				$lines = implode('', $lines);
				$count++;
			} else {
				$lines = $this->output;
				$count++;
			}
			switch($tag)
			{
			case 'recipe':
				$lines = str_replace('<recipe name>', $attributes['NAME'], $lines);
				break;
			case 'cooker':
				$lines = str_replace('<cooker>', $data, $lines);
				break;
			case 'email':
				$lines = str_replace('<email>', $data, $lines);
				break;
			case 'ingredient':
				static $ings = 0;
				$ings++;
				if(!isset($this->ing_loop)) {
					$x = explode('<LOOP ingredients>', $lines);
					$y = explode('</LOOP ingredients>', $x[1]);
					$x = explode('<ingredient>', $y[0]);
					$this->ing_loop = $x[1] . $x[0] . '<ingredient>';
				}
				if($ings < $this->ingredients)
					$lines = str_replace('<ingredient>', $data . $this->ing_loop, $lines);
				elseif($ings == $this->ingredients)
					$lines = str_replace('<ingredient>', $data, $lines);
				$lines = str_replace(array('<LOOP ingredients>' . "\n", '</LOOP ingredients>' . "\n"), '', $lines);
				break;
			case 'supply':
				static $sups = 0;
				$sups++;
				if(!isset($this->sup_loop)) {
					$x = explode('<LOOP supplies>', $lines);
					$y = explode('</LOOP supplies>', $x[1]);
					$x = explode('<supply>', $y[0]);
					$this->sup_loop = $x[1] . $x[0] . '<supply>';
				}
				if($sups < $this->supplies)
					$lines = str_replace('<supply>', $data . $this->sup_loop, $lines);
				elseif($sups == $this->supplies)
					$lines = str_replace('<supply>', $data, $lines);
				$lines = str_replace(array('<LOOP supplies>' . "\n", '</LOOP supplies>' . "\n"), '', $lines);
				break;
			case 'steps':
				$data = nl2br($data);
				$lines = str_replace('<steps>', $data, $lines);
				break;
			case 'image':
				$img = '<img src="images/' . $data . '" width="' . $attributes['WIDTH'] . '" height="' . $attributes['HEIGHT'] . '" alt="' . $attributes['ALT'] . '" />';
				$lines = str_replace('<image>', $img, $lines);
			}
			$lines = str_replace("\n\n", "\n", $lines);
			$this->SetOutput($lines);
		}
	}

	function Destroy()
	{
		print($this->output);
		xml_parser_free($this->parser);
	}
}
?>
Return current item: phpCookBook