XML.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00042 class XML
00043 {
00050      var $xmlText = '';
00051 
00052 
00058      var $root = 'xml';
00059 
00060      /*
00061       * Char to indent with.
00062       *
00063       * @var String
00064       */
00065      var $indentChar = "\t";
00066 
00067 
00072      var $nl = "\n";
00073      
00080      function encode($array)
00081      {
00082           //star and end the XML document
00083           $this->xmlText = '<?xml version="1.0" encoding="utf-8"?>'.$this->nl;
00084           $this->xmlText .= '<'.$this->root.'>'.$this->nl;
00085           $this->array_transform($array,1);
00086           $this->xmlText .='</'.$this->root.'>';
00087           
00088           return $this->xmlText;
00089      }
00090 
00091 
00095      function array_transform($array,$depth){
00096 
00097           foreach($array as $key => $value)
00098           {
00099                $attr = array();
00100                if   ( is_numeric($key) )
00101                {
00102                     // Array-Einträge mit numerischen Index können nicht direkt in ein XML-Element
00103                     // umgewandelt werden, da nur-numerische Element-Namen nicht erlaubt sind.
00104                     // Daher verwenden wir dann 'entry' als Elementnamen.
00105                     $attr['id'] = $key;
00106                     $key = 'entry';
00107                }
00108                
00109                $indent = str_repeat($this->indentChar,$depth);
00110                
00111                if   ( empty($value) )
00112                {
00113                     $this->xmlText .= $indent.$this->shortTag($key,$attr).$this->nl;
00114                }
00115                elseif    ( is_object($value) )
00116                {
00117                     // Der Inhalt ist ein Array, daher rekursiv verzweigen.
00118                     $this->xmlText .= $indent.$this->openTag($key,$attr).$this->nl;
00119                     $prop = get_object_vars($value);
00120                     $this->array_transform($prop,$depth+1); // Rekursiver Aufruf
00121                     $this->xmlText .= $indent.$this->closeTag($key).$this->nl;
00122                }
00123                elseif    ( is_array($value) )
00124                {
00125                     // Der Inhalt ist ein Array, daher rekursiv verzweigen.
00126                     $this->xmlText .= $indent.$this->openTag($key,$attr).$this->nl;
00127                     $this->array_transform($value,$depth+1); // Rekursiver Aufruf
00128                     $this->xmlText .= $indent.$this->closeTag($key).$this->nl;
00129                }
00130                else
00131                {
00132                     // Der Inhalt ist ein einfacher Inhalt (kein Array).
00133                     $this->xmlText .= $indent.$this->openTag($key,$attr);
00134                     $this->xmlText .= $value;
00135                     $this->xmlText .= $this->closeTag($key).$this->nl;
00136                }
00137           }
00138      }
00139      
00140      
00141      function openTag($key,$attr)
00142      {
00143           $tag = '<'.$key;
00144           foreach( $attr as $attr_name=>$attr_value )
00145                $tag .= ' '.$attr_name.'="'.$attr_value.'"';
00146           $tag .= '>';
00147           return $tag;
00148      }
00149      
00150      
00151      
00152      function shortTag($key,$attr)
00153      {
00154           $tag = '<'.$key;
00155           foreach( $attr as $attr_name=>$attr_value )
00156                $tag .= ' '.$attr_name.'="'.$attr_value.'"';
00157           $tag .= ' />';
00158           return $tag;
00159      }
00160      
00161      
00162      
00163      function closeTag($key)
00164      {
00165           return '</'.$key.'>';
00166      }
00167 }
00168 
00169 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8