Location: PHPKode > scripts > XML 2 PHP and PHP 2 XML > xml-2-php-and-php-2-xml/class_xmlparser.php
<?php

/**
 * Simple XML Parser. XML to PHP, PHP to XML
 *
 * @name    XML Parser
 * @author  Marcus Vinícius <hide@address.com>
 * @version 1.0 2005-04-25
 * @example 
 *
 * $xmlparser = new XML_Parser();
 * 
 * $xmlparser->setCharset("ISO-8859-1");
 * $xmlparser->xml2php("example.xml");
 * $xmlparser->php2xml();
 * $xmlparser->outputXML();
 *
 */
class XML_Parser {

	var $charset = "UTF-8";
	var $xml, $arr_xml;


	/**
	 * Get contents from XML file and insert into an array
	 *
	 * @param string $file
	 * @return mixed
	 */
	function xml2php($file) {

		$xml_parser = xml_parser_create();
		if (!$fp = fopen($file, "r")) {
			die("unable to open XML");
		}
		$contents = fread($fp, filesize($file));
		fclose($fp);
		xml_parse_into_struct($xml_parser, $contents, $arr_vals);
		xml_parser_free($xml_parser);

		$this->arr_xml = $arr_vals;

	}


	/**
	 * Convert php array to XML
	 *
	 * @return string
	 */
	function php2xml() {

		$array_haystack = $this->arr_xml;

		$xml = "<?xml version=\"1.0\" encoding=\"{$this->charset}\"?>\n";

		if ((!empty($array_haystack)) AND (is_array($array_haystack))) {

			foreach ($array_haystack as $xml_key => $xml_value) {

				switch ($xml_value['type']) {
					case "open":
					$xml .= str_repeat("\t", $xml_value['level'] - 1);
					$xml .= "<" . strtolower($xml_value['tag']);
					$xml .= (!isset($xml_value['attributes']))? ">\n": false;
					break;

					case "complete":
					$xml .= str_repeat("\t", $xml_value['level'] - 1);
					$xml .= "<" . strtolower($xml_value['tag']);
					$xml .= (!isset($xml_value['attributes']))? ">": false;
					break;

					case "close":
					$xml .= str_repeat("\t", $xml_value['level'] - 1);
					$xml .= "</" . strtolower($xml_value['tag']);
					$xml .= (!isset($xml_value['attributes']))? ">\n": false;
					break;

					default:
					break;

				}

				if (isset($xml_value['value'])) {

					$xml_value['value'] = trim($xml_value['value']);
					$xml_value['value'] = htmlspecialchars($xml_value['value'], ENT_QUOTES);
					$xml_value['value'] = str_replace("\r", "&#10;", $xml_value['value']);
					$xml_value['value'] = str_replace("\n", "&#13;", $xml_value['value']);

				}

				if (isset($xml_value['attributes'])) {

					foreach ($xml_value['attributes'] as $atribute_key => $atribute_value) {
						$xml .= sprintf(' %s="%s"', strtolower($atribute_key), htmlspecialchars(trim($atribute_value), ENT_QUOTES));
					}

					if ((empty($xml_value['value'])) AND ($xml_value['type'] == "complete")) {
						$xml .= " />\n";
					} else if ((!empty($xml_value['value'])) AND ($xml_value['type'] == "complete")) {
						$xml .= ">";
					} else {
						$xml .= ">\n";
					}

				}

				if (!empty($xml_value['value'])) {

					$xml .= sprintf("%s</%s>\n", $xml_value['value'], strtolower($xml_value['tag']));

				}

			}

		}

		$this->xml = $xml;

	}


	/**
	 * Output content as XML
	 *
	 * @param string $content
	 */
	function outputXML() {

		header("Content-Type: application/xml; charset={$this->charset}");
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");

		print $this->xml;

	}


	/**
	 * Set charset
	 *
	 * @param string $charset
	 */
	function setCharset($charset) {

		if (!empty($charset)) {
			$this->charset = $charset;
		}

	}

}

?>
Return current item: XML 2 PHP and PHP 2 XML