openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

XML.class.php (4529B)


      1 <?php
      2 
      3 namespace util;
      4 /**
      5  * Multidimensional Array-to-XML.
      6  *
      7  * Example:
      8  * $xml = new XML();
      9  * header('Content-Type: application/xml');
     10  * echo $xml->encode( $yourBigArray );
     11  * exit;
     12  *
     13  * Author: Honor� Vasconcelos, Jan Dankert
     14  *
     15  * Original from:
     16  * Clean XML To Array: http://www.phpclasses.org/browse/package/3598.html
     17  *
     18  * License of this class: BSD-Licence.
     19  *
     20  * Redistribution and use in source and binary forms, with or without modification,
     21  * are permitted provided that the following conditions are met:
     22  * Redistributions of source code must retain the above copyright notice, this list
     23  * of conditions and the following disclaimer.
     24  * Redistributions in binary form must reproduce the above copyright notice, this
     25  * list of conditions and the following disclaimer in the documentation and/or other
     26  * materials provided with the distribution.
     27  *
     28  * Neither the name of the Author(s) nor the names of its contributors may be used
     29  * to endorse or promote products derived from this software without specific prior
     30  * written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     36  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     37  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     38  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     39  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     40  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     41  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     42  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     43  */
     44 class XML
     45 {
     46 	/**
     47 	 * Parse multidimentional array to XML.
     48 	 *
     49 	 * @param array $array
     50 	 * @return String
     51 	 */
     52 	var $xmlText = '';
     53 
     54 
     55 	/**
     56 	 * Name of the root element.
     57 	 *
     58 	 * @var String
     59 	 */
     60 	var $root = 'xml';
     61 
     62 	/*
     63 	 * Char to indent with.
     64 	 *
     65 	 * @var String
     66 	 */
     67 	var $indentChar = "\t";
     68 
     69 
     70 	/**
     71 	 * Newline-Char
     72 	 * @var String
     73 	 */
     74 	var $nl = "\n";
     75 
     76 	/**
     77 	 * Encode a array to XML.
     78 	 *
     79 	 * @param Array $array
     80 	 * @return String (serialized XML)
     81 	 */
     82 	function encode($array)
     83 	{
     84 		//star and end the XML document
     85 		$this->xmlText = '<?xml version="1.0" encoding="utf-8"?>' . $this->nl;
     86 		$this->xmlText .= '<' . $this->root . '>' . $this->nl;
     87 		$this->array_transform($array, 1);
     88 		$this->xmlText .= '</' . $this->root . '>';
     89 
     90 		return $this->xmlText;
     91 	}
     92 
     93 
     94 	/**
     95 	 * @access private
     96 	 */
     97 	function array_transform($array, $depth)
     98 	{
     99 
    100 		foreach ($array as $key => $value) {
    101 			$attr = array();
    102 			if (is_numeric($key)) {
    103 				// Array-Einträge mit numerischen Index können nicht direkt in ein XML-Element
    104 				// umgewandelt werden, da nur-numerische Element-Namen nicht erlaubt sind.
    105 				// Daher verwenden wir dann 'entry' als Elementnamen.
    106 				$attr['id'] = $key;
    107 				$key = 'entry';
    108 			}
    109 
    110 			$indent = str_repeat($this->indentChar, $depth);
    111 
    112 			if (empty($value)) {
    113 				$this->xmlText .= $indent . $this->shortTag($key, $attr) . $this->nl;
    114 			} elseif (is_object($value)) {
    115 				// Der Inhalt ist ein Array, daher rekursiv verzweigen.
    116 				$this->xmlText .= $indent . $this->openTag($key, $attr) . $this->nl;
    117 				$prop = get_object_vars($value);
    118 				$this->array_transform($prop, $depth + 1); // Rekursiver Aufruf
    119 				$this->xmlText .= $indent . $this->closeTag($key) . $this->nl;
    120 			} elseif (is_array($value)) {
    121 				// Der Inhalt ist ein Array, daher rekursiv verzweigen.
    122 				$this->xmlText .= $indent . $this->openTag($key, $attr) . $this->nl;
    123 				$this->array_transform($value, $depth + 1); // Rekursiver Aufruf
    124 				$this->xmlText .= $indent . $this->closeTag($key) . $this->nl;
    125 			} else {
    126 				// Der Inhalt ist ein einfacher Inhalt (kein Array).
    127 				$this->xmlText .= $indent . $this->openTag($key, $attr);
    128 				$this->xmlText .= $value;
    129 				$this->xmlText .= $this->closeTag($key) . $this->nl;
    130 			}
    131 		}
    132 	}
    133 
    134 
    135 	function openTag($key, $attr)
    136 	{
    137 		$tag = '<' . $key;
    138 		foreach ($attr as $attr_name => $attr_value)
    139 			$tag .= ' ' . $attr_name . '="' . $attr_value . '"';
    140 		$tag .= '>';
    141 		return $tag;
    142 	}
    143 
    144 
    145 	function shortTag($key, $attr)
    146 	{
    147 		$tag = '<' . $key;
    148 		foreach ($attr as $attr_name => $attr_value)
    149 			$tag .= ' ' . $attr_name . '="' . $attr_value . '"';
    150 		$tag .= ' />';
    151 		return $tag;
    152 	}
    153 
    154 
    155 	function closeTag($key)
    156 	{
    157 		return '</' . $key . '>';
    158 	}
    159 }
    160 
    161 ?>