All pastes #497970 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-20 04:14 UTC
rendered paste body
<?php

	class XML extends XML_Node
	{

		var $_version = '1.0';
		var $_encoding = 'ISO-8859-1';
		var $_stylesheet = array('href' => '', 'type' => '');
		
		function XML($rootName, $attributes = array(), $encoding = 'ISO-8859-1')
		{
			$this->XML_Node($rootName, $attributes);
			$this->setEncoding($encoding);
		}
		
		function setEncoding($encoding)
		{
			$this->_encoding = $encoding;
		}
		
		function setVersion($version)
		{
			$this->_version = $version;
		}
		
		function setStylesheet($href, $type = 'text/xsl')
		{
			$this->_stylesheet['href'] = $href;
			$this->_stylesheet['type'] = $type;
		}
		
		function output($return = false)
		{
			if ($return)
			{
				$data = <<<XML
<?xml version="{$this->_version}" encoding="{$this->_encoding}"?>
<?xml-stylesheet type="{$this->_stylesheet['type']}" href="{$this->_stylesheet['href']}"?>

XML;
				$this->compile();
				$data .= $this->getXML();
			}
			else
			{
				header('Content-Type: text/xml; charset: ' . $this->_encoding);
				print <<<XML
<?xml version="{$this->_version}" encoding="{$this->_encoding}"?>
<?xml-stylesheet type="{$this->_stylesheet['type']}" href="{$this->_stylesheet['href']}"?>

XML;
				$this->compile();
				print $this->getXML();
			}
		}
		
	}
	
	class XML_Node
	{
		
		var $_innerXML;
		var $_name;
		var $_data;
		var $_childs = array();
		var $_attributes = array();
		var $_text_nodes = array();
		
		function XML_Node($name, $attributes = array(), $data = '')
		{
			$this->_name = $name;
			$this->_data = $data;

			if (sizeof($attributes) > 0)
			{
				foreach($attributes as $name => $value)
				{
					$this->addAttribute($name, $value);
				}
			}
		}
		
		function setData($data)
		{
			$this->_data = $data;
		}
		
		function addAttribute($name, $value)
		{
			$this->_attributes[$name] = str_replace('"', '\"', $value);
		}
		
		function appendChild($node)
		{
			$this->_childs[] = $node;
		}
		
		function compile()
		{
			if (sizeof($this->_childs) > 0)
			{
				for($i = 0; $i < sizeof($this->_childs); $i++)
				{
					$this->_childs[$i]->compile();
					$this->_innerXML .= $this->_childs[$i]->getXML();
				}
			}
		}
		
		function getXML()
		{
			$xml = '<' . $this->_name;
			
			if (sizeof($this->_attributes) > 0)
			{
				foreach($this->_attributes as $key => $value)
				{
					$xml .= ' ' . $key . '="' . $value . '"';
				}
			}
			
			$xml .= '>';
			
			$hasChilds = (sizeof($this->_childs) > 0);
			
			if ($hasChilds)
			{
				$xml .= "\n";
				$xml .= $this->_innerXML;
			}
			else 
			{
				$xml .= $this->_data;
			}

			$xml .= '</' . $this->_name . '>' . "\n";
			
			return $xml;
		}
		
		function appendArray($name, $array)
		{
			if (sizeof($array) > 0)
			{
					foreach($array as $entry)
					{
						$node = new XML_Node($name);
						
						if (sizeof($entry) > 0)
						{
							foreach($entry as $field_name => $field_value)
							{
								$node->appendChild(new XML_Text_Node($field_name, $field_value));
							}
						}
						
						$this->appendChild($node);
					}
			}
		}
		
	}
	
	class XML_Text_Node extends XML_Node
	{
		function XML_Text_Node($name, $value)
		{
			$this->XML_Node($name, array(), $value);
		}
	}

?>