<?php
class u007xml
{
	public $arrOutput = [];
	public $resParser = null;
	public $strXmlData = null;

	public function u007xml1($tfile = '')
	{
		if (trim($tfile) != '') {
			$this->loadFile($tfile);
		}
	}

	public function loadFile($tfile)
	{
		$this->thefile = $tfile;
		$th = file($tfile);
		$tdata = implode("\n", $th);
		return $this->parse($tdata);
	}

	public function loadString($tstring)
	{
		$tdata = $tstring;
		return $this->parse($tdata);
	}

	public function parse($strInputXML)
	{
		$this->resParser = xml_parser_create();
		xml_set_object($this->resParser, $this);
		xml_set_element_handler($this->resParser, 'tagOpen', 'tagClosed');
		xml_set_character_data_handler($this->resParser, 'tagData');
		$this->strXmlData = xml_parse($this->resParser, $strInputXML);

		if (!$this->strXmlData) {
			exit(sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($this->resParser)), xml_get_current_line_number($this->resParser)));
		}

		xml_parser_free($this->resParser);
		return $this->arrOutput;
	}

	public function tagOpen($parser, $name, $attrs)
	{
		$tag = ['nodename' => $name, 'attributes' => $attrs];
		array_push($this->arrOutput, $tag);
	}

	public function tagData($parser, $tagData)
	{
		if (trim($tagData)) {
			if (isset($this->arrOutput[count($this->arrOutput) - 1]['nodevalue'])) {
				$this->arrOutput[count($this->arrOutput) - 1]['nodevalue'] .= $this->parseXMLValue($tagData);
			}
			else {
				$this->arrOutput[count($this->arrOutput) - 1]['nodevalue'] = $this->parseXMLValue($tagData);
			}
		}
	}

	public function tagClosed($parser, $name)
	{
		$this->arrOutput[count($this->arrOutput) - 2]['childrens'][] = $this->arrOutput[count($this->arrOutput) - 1];

		if (count($this->arrOutput[count($this->arrOutput) - 2]['childrens']) == 1) {
			$this->arrOutput[count($this->arrOutput) - 2]['firstchild'] = &$this->arrOutput[count($this->arrOutput) - 2]['childrens'][0];
		}

		array_pop($this->arrOutput);
	}

	public function toArray()
	{
	}

	public function parseXMLValue($tvalue)
	{
		$tvalue = htmlentities($tvalue);
		return $tvalue;
	}

	public function toXML(&$tob = NULL)
	{
		$result = '';

		if ($tob == NULL) {
			$tob = $this->arrOutput;
		}

		if (!isset($tob)) {
			echo 'XML Array empty...';
			return NULL;
		}

		for ($c = 0; $c < count($tob); $c++) {
			$result .= '<' . $tob[$c]['nodename'];

			while (list($key, $value) = each($tob[$c]['attributes'])) {
				$result .= ' ' . $key . '="' . $this->parseXMLValue($value) . '"';
			}

			$result .= '>';

			if (isset($tob[$c]['nodevalue'])) {
				$result .= $tob[$c]['nodevalue'];
			}

			if (0 < count($tob[$c]['childrens'])) {
				$result .= "\r\n" . $this->toXML($tob[$c]['childrens']) . '';
			}

			$result .= '</' . $tob[$c]['nodename'] . '>' . "\r\n";
		}

		return $result;
	}

	public function displayXML()
	{
		print_r($this->arrOutput);
	}

	public function getXML($tob = NULL)
	{
		return '<?xml version=\'1.0\'?>' . "\r\n" . $this->toXML($tob);
	}
}

function xml2array($contents, $get_attributes = 1, $priority = 'tag')
{
	if (!function_exists('xml_parser_create')) {
		return [];
	}

	$parser = xml_parser_create('');
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
	xml_parse_into_struct($parser, trim($contents), $xml_values);
	xml_parser_free($parser);

	if (!$xml_values) {
		return NULL;
	}

	foreach ($xml_values as &$data) {
		$pos = strpos($data['tag'], ':');

		if (0 < $pos) {
			$nk = substr($data['tag'], $pos + 1);
			$data['tag'] = $nk;
		}
	}

	unset($data);
	$xml_array = [];
	$parents = [];
	$opened_tags = [];
	$arr = [];
	$current = &$xml_array;
	$repeated_tag_index = [];

	foreach ($xml_values as $data) {
		unset($attributes, $value);
		extract($data);
		$result = [];
		$attributes_data = [];

		if (isset($value)) {
			if ($priority == 'tag') {
				$result = $value;
			}
			else {
				$result['value'] = $value;
			}
		}
		if (isset($attributes) && $get_attributes) {
			foreach ($attributes as $attr => $val) {
				if ($priority == 'tag') {
					$attributes_data[$attr] = $val;
				}
				else {
					$result['attr'][$attr] = $val;
				}
			}
		}

		if ($type == 'open') {
			$parent[$level - 1] = &$current;
			if (!is_array($current) || !in_array($tag, array_keys($current))) {
				$current[$tag] = $result;

				if ($attributes_data) {
					$current[$tag . '_attr'] = $attributes_data;
				}

				$repeated_tag_index[$tag . '_' . $level] = 1;
				$current = &$current[$tag];
			}
			else {
				if (isset($current[$tag][0])) {
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
					$repeated_tag_index[$tag . '_' . $level]++;
				}
				else {
					$current[$tag] = [$current[$tag], $result];
					$repeated_tag_index[$tag . '_' . $level] = 2;

					if (isset($current[$tag . '_attr'])) {
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
						unset($current[$tag . '_attr']);
					}
				}

				$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
				$current = &$current[$tag][$last_item_index];
			}
		}
		else if ($type == 'complete') {
			if (!isset($current[$tag])) {
				$current[$tag] = $result;
				$repeated_tag_index[$tag . '_' . $level] = 1;
				if (($priority == 'tag') && $attributes_data) {
					$current[$tag . '_attr'] = $attributes_data;
				}
			}
			else if (isset($current[$tag][0]) && is_array($current[$tag])) {
				$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
				if (($priority == 'tag') && $get_attributes && $attributes_data) {
					$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
				}

				$repeated_tag_index[$tag . '_' . $level]++;
			}
			else {
				$current[$tag] = [$current[$tag], $result];
				$repeated_tag_index[$tag . '_' . $level] = 1;
				if (($priority == 'tag') && $get_attributes) {
					if (isset($current[$tag . '_attr'])) {
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
						unset($current[$tag . '_attr']);
					}

					if ($attributes_data) {
						$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
					}
				}

				$repeated_tag_index[$tag . '_' . $level]++;
			}
		}
		else if ($type == 'close') {
			$current = &$parent[$level - 1];
		}
	}

	return $xml_array;
}

?>