Location: PHPKode > projects > OpenNitro > trunk/Nitro/XML/XMLParser.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: XML :: XMLParser                                                 |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2004 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This source file is copyrighted by June Systems BV, the Netherlands       |
// | If you would like to use this file in your projects, please contact       |
// | hide@address.com                                                     |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: XMLParser.inc.php 229 2008-04-17 09:20:31Z oli $
//
// This file contains the base functions and initialization for Nitro
//

/**
 * This is the Nitro XML parser class and functions
 *
 * @package			Nitro
 * @subpackage	XML
 * @author 			Siggi Oskarsson
 * @version 		$Revision: 1.3 $
 * @copyright		2004 June Systems BV
 */

/**
 * XMLParser class
 *
 * This class parses an XML string or XML file into an array. This array
 * can be accessed via the access functions of the class.
 *
 * @package			Nitro
 * @subpackage	XML
 * @access public
 */
class XMLParser {
	var $xml_parser;
	var $XML;
	var $XMLPath = Array();
	var $LastPop;
	
	/**
	 * XMLParser class constructor
	 *
	 * @access public
	 */
	function XMLParser() {
	}

	/**
	 * Parse valid XML file
	 *
	 * This function loads the XML of the class from an XML file.
	 * It uses Parse() to parse the data of the file.
	 *
	 * @access public
	 */
	function ParseFile($File) {
		if (!($fp = fopen($File, "r"))) {
		   die("could not open XML input");
		}
		while ($data = fread($fp, 4096)) {
			$XMLData.= $data;
		}
		$this->Parse($XMLData);
	}

	/**
	 * Parse valid XML string data
	 *
	 * This function loads the XML of the class from an XML string.
	 *
	 * @access public
	 */
	function Parse($XMLData) {
		$this->xml_parser = xml_parser_create();
		xml_set_object($this->xml_parser, &$this);
		xml_set_element_handler($this->xml_parser, "XMLstartElement", "XMLendElement");
		xml_set_character_data_handler($this->xml_parser, "XMLData"); 

		if (!xml_parse($this->xml_parser, $XMLData)) {
		   die(sprintf("XML error: %s at line %d",
		               xml_error_string(xml_get_error_code($this->xml_parser)),
		               xml_get_current_line_number($this->xml_parser)));
		}
		xml_parser_free($this->xml_parser);
	}

	/**
	 * Get the XML parsed array of the class
	 *
	 * This function returns the parsed array of the class.
	 *
	 * @return	array	XML data array
	 * @access public
	 */
	function GetXML(){
		return $this->XML;
	}

	/**
	 * Get a part of the parsed XML
	 *
	 * This function returns a part of the parsed XML array. If the element
	 * counters are not specified, it assumes [0].
	 * Format: /DOCROOT/SUB1/SUB2
	 *         DOCROOT/SUB1/SUB2
	 *         DOCROOT[0]/SUB1[1]/SUB2[0]
	 *
	 * @param	string	$Path Path of XML string to return (ex. DOCROOT/SUB1/SUB2)
	 * @return	array	XML data array part
	 * @access public
	 */
	function GetTree($Path) {
		// Trim leading / if exists
		if (ereg("^/", $Path)) {
			$Path = substr($Path,1,-1);
		}
		// create useable array
		$Path = explode("/", $Path);
		// walk through array to create array path
		$ArrPath = "";
		foreach($Path AS $P) {
			if (ereg("\[[0-9]+\]", $P)) {
				
			}
		}
	}

	/**
	 * Internal function for parsing the XML
	 *
	 * @param	$parser	XML parser resource
	 * @param	$name	Name of element being parsed
	 * @param	$attrs	Element attributes
	 * @access private
	 */
	function XMLstartElement($parser, $name, $attrs) {
		array_push($this->XMLPath, $name);
		$path = "";
		$prepath = "";
		$n = 1;
		foreach($this->XMLPath AS $P) {
			$prepath = $path."['$P']";
			if ($this->LastPop == $name && $name == $P) {
				$i = eval('return count($this->XML'.$prepath.');');
			} else {
				$i = eval('return count($this->XML'.$prepath.') - 1;');
			}
			if ($i < 0) $i = 0;
			$path.= "['$P'][$i]";
			$n++;
		}
		eval ('$this->XML'.$path.'["Attributes"] = $attrs;');
	}
	
	/**
	 * Internal function for parsing the XML
	 *
	 * @param	$parser	XML parser resource
	 * @param	$data	Value of element currently being read
	 * @access private
	 */
	function XMLData($parser, $data){
		if (trim($data)) {
			$path = "";
			$prepath = "";
			$n = 1;
			foreach($this->XMLPath AS $P) {
				$prepath = $path."['$P']";
				if ($this->LastPop == $name && $name == $P) {
					$i = eval('return count($this->XML'.$prepath.');');
				} else {
					$i = eval('return count($this->XML'.$prepath.') - 1;');
				}
				if ($i < 0) $i = 0;
				$path.= "['$P'][$i]";
				$n++;
			}
			eval ('$this->XML'.$path.'["Value"] = $data;');
		}
	}
	
	/**
	 * Internal function for parsing the XML
	 *
	 * @param	$parser	XML parser resource
	 * @param	$name	Name of element being parsed
	 * @access private
	 */
	function XMLendElement($parser, $name) {
		$this->LastPop = $name;
		array_pop($this->XMLPath);
	}
}
?>
Return current item: OpenNitro