Snippets-logo-small
  • TYPO3 Typoscript
  • TYPO3 TSConfig
  • TYPO3 Install Tool
  • TYPO3 allg
  • Git
  • Javascript - JQuery
  • Unix
  • css
  • regular expressions
  • php
  • SQL
  • Ruby on Rails
  • array2xml

array2xml

  • function array2xml 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    
    /**
     * Simple array to XML conversion.
     *
     * Warning: Ignores all attributes!
     *
     * Link: http://www.johnro.net/?p=79
     *
     * @author johnro
     * @author Ruben Barkow
     * @param array $array
     * @param integer $depth
     * @param string $namespace
     */
    function array2xml($array, $depth=0, $namespace = '') {
            $nl="
    ";
            $tab="        ";
            $xml = '';
            foreach($array as $key => $value) {
                    if(is_array($value)) {
                                    $value = array2xml($value,$depth+1);
                    }
                    if(is_numeric($key)){
                            $key_start='item nr="'.$key.'"';
                            $key_end='item';
                    }else {
                            $key_start=$key;
                            $key_end=$key;
                    }
                    if(isset($value[0]) and $value[0]=='<') {
                            // childnote:
                            $xml .= $nl.str_repeat($tab,$depth)."" . $nl.str_repeat($tab,$depth+1) . ($value) . $nl . str_repeat($tab,$depth) . "";
                    } else $xml .= $nl.str_repeat($tab,$depth)."" . XMLStrFormat($value) . "";
            }
            
            return trim($xml);
    }
    
    function XMLStrFormat($str){
            /* In XML gilt wie in HTML: Zeichen, die bei der XML-Syntax besondere Bedeutung haben,
                    müssen Sie umschreiben, wenn Sie sie im normalen Text zwischen den Tags verwenden wollen.
                    Folgende Zeichen sind betroffen:
            Zeichen         Notation in XML
                     >
            &         &
            "         "
            '         '
    
            */
            $str = str_replace("&", "&", $str);
            $str = str_replace("", ">", $str);
            $str = str_replace("'", "'", $str);
            $str = str_replace(""", """, $str);
            $str = str_replace("
    ", "", $str);
            // xml generates an error for some characters below Ascii(32) (space):
            $str = str_replace(chr(11), "", $str);
            $str = strtr($str,$GLOBALS['asc2uni']);
            return $str;
    }
    
    $GLOBALS['asc2uni'] = Array();
    for($i=128;$i<256;$i++){
      $GLOBALS['asc2uni'][chr($i)] = "&#x".dechex($i).";";
    }
    by ruben
This site is made by tan3