3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array('foo' => 'bar', 'zoo' => array(0,1,2,3,4,5,6)); /** * Array2XML: A class to convert array in PHP to XML * It also takes into account attributes names unlike SimpleXML in PHP * It returns the XML in form of DOMDocument class for further manipulation. * It throws exception if the tag name or attribute name has illegal chars. * * Author : Lalit Patel * Website: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes * License: Apache License 2.0 * http://www.apache.org/licenses/LICENSE-2.0 * Version: 0.1 (10 July 2011) * Version: 0.2 (16 August 2011) * - replaced htmlentities() with htmlspecialchars() (Thanks to Liel Dulev) * - fixed a edge case where root node has a false/null/0 value. (Thanks to Liel Dulev) * Version: 0.3 (22 August 2011) * - fixed tag sanitize regex which didn't allow tagnames with single character. * Version: 0.4 (18 September 2011) * - Added support for CDATA section using @cdata instead of @value. * Version: 0.5 (07 December 2011) * - Changed logic to check numeric array indices not starting from 0. * Version: 0.6 (04 March 2012) * - Code now doesn't @cdata to be placed in an empty array * Version: 0.7 (24 March 2012) * - Reverted to version 0.5 * Version: 0.8 (02 May 2012) * - Removed htmlspecialchars() before adding to text node or attributes. * * Usage: * $xml = Array2XML::createXML('root_node_name', $php_array); * echo $xml->saveXML(); */ class Array2XML { private static $xml = null; private static $encoding = 'UTF-8'; /** * Initialize the root XML node [optional] * @param $version * @param $encoding * @param $format_output */ public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { self::$xml = new DomDocument($version, $encoding); self::$xml->formatOutput = $format_output; self::$encoding = $encoding; } /** * Convert an Array to XML * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return DomDocument */ public static function &createXML($node_name, $arr=array()) { $xml = self::getXMLRoot(); $xml->appendChild(self::convert($node_name, $arr)); self::$xml = null; // clear the xml node in the class for 2nd time use. return $xml; } /** * Convert an Array to XML * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return DOMNode */ private static function &convert($node_name, $arr=array()) { //print_arr($node_name); $xml = self::getXMLRoot(); $node = $xml->createElement($node_name); if(is_array($arr)){ // get the attributes first.; if(isset($arr['@attributes'])) { foreach($arr['@attributes'] as $key => $value) { if(!self::isValidTagName($key)) { throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); } $node->setAttribute($key, self::bool2str($value)); } unset($arr['@attributes']); //remove the key from the array once done. } // check if it has a value stored in @value, if yes store the value and return // else check if its directly stored as string if(isset($arr['@value'])) { $node->appendChild($xml->createTextNode(self::bool2str($arr['@value']))); unset($arr['@value']); //remove the key from the array once done. //return from recursion, as a note with value cannot have child nodes. return $node; } else if(isset($arr['@cdata'])) { $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); unset($arr['@cdata']); //remove the key from the array once done. //return from recursion, as a note with cdata cannot have child nodes. return $node; } } //create subnodes using recursion if(is_array($arr)){ // recurse to get the node for that key foreach($arr as $key=>$value){ if(!self::isValidTagName($key)) { throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); } if(is_array($value) && is_numeric(key($value))) { // MORE THAN ONE NODE OF ITS KIND; // if the new array is numeric index, means it is array of nodes of the same kind // it should follow the parent key name foreach($value as $k => $v) { $node->appendChild(self::convert($key, 'item')); $node->appendChild(self::convert($key, $v)); } } else { // ONLY ONE NODE OF ITS KIND $node->appendChild(self::convert($key, $value)); } unset($arr[$key]); //remove the key from the array once done. } } // after we are done with all the keys in the array (if it is one) // we check if it has any text value, if yes, append it. if(!is_array($arr)) { $node->appendChild($xml->createTextNode(self::bool2str($arr))); } return $node; } /* * Get the root XML node, if there isn't one, create it. */ private static function getXMLRoot(){ if(empty(self::$xml)) { self::init(); } return self::$xml; } /* * Get string representation of boolean value */ private static function bool2str($v){ //convert boolean to text value. $v = $v === true ? 'true' : $v; $v = $v === false ? 'false' : $v; return $v; } /* * Check if the tag name or attribute name contains illegal characters * Ref: http://www.w3.org/TR/xml/#sec-common-syn */ private static function isValidTagName($tag){ $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; } } $xml = Array2XML::createXML('root-element-here', $data); echo $xml->saveXML(); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kgnbt
function name:  (null)
number of ops:  10
compiled vars:  !0 = $data, !1 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
  168     1        INIT_STATIC_METHOD_CALL                                  'Array2XML', 'createXML'
          2        SEND_VAL                                                 'root-element-here'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !1, $3
  170     6        INIT_METHOD_CALL                                         !1, 'saveXML'
          7        DO_FCALL                                      0  $5      
          8        ECHO                                                     $5
  171     9      > RETURN                                                   1

Class Array2XML:
Function init:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kgnbt
function name:  init
number of ops:  15
compiled vars:  !0 = $version, !1 = $encoding, !2 = $format_output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV_INIT                                        !0      '1.0'
          1        RECV_INIT                                        !1      'UTF-8'
          2        RECV_INIT                                        !2      <true>
   48     3        NEW                                              $4      'DomDocument'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
          7        ASSIGN_STATIC_PROP                                       'xml'
          8        OP_DATA                                                  $4
   49     9        FETCH_STATIC_PROP_W          unknown             $6      'xml'
         10        ASSIGN_OBJ                                               $6, 'formatOutput'
         11        OP_DATA                                                  !2
   50    12        ASSIGN_STATIC_PROP                                       'encoding'
         13        OP_DATA                                                  !1
   51    14      > RETURN                                                   null

End of function init

Function createxml:
Finding entry points
Branch analysis from position: 0
Return found
filename:       /in/Kgnbt
function name:  createXML
number of ops:  16
compiled vars:  !0 = $node_name, !1 = $arr, !2 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   60     2        INIT_STATIC_METHOD_CALL                                  'getXMLRoot'
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !2, $3
   61     5        INIT_METHOD_CALL                                         !2, 'appendChild'
          6        INIT_STATIC_METHOD_CALL                                  'convert'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0  $5      
         10        SEND_VAR_NO_REF_EX                                       $5
         11        DO_FCALL                                      0          
   63    12        ASSIGN_STATIC_PROP                                       'xml'
         13        OP_DATA                                                  null
   64    14      > RETURN_BY_REF                                            !2
   65    15*     > RETURN_BY_REF                                            null

End of function createxml

Function convert:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 70
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 39
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 37
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 37
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 55
Branch analysis from position: 41
Return found
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 70
Branch analysis from position: 57
Return found
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 127
Branch analysis from position: 72
2 jumps found. (Code = 77) Position 1 = 73, Position 2 = 126
Branch analysis from position: 73
2 jumps found. (Code = 78) Position 1 = 74, Position 2 = 126
Branch analysis from position: 74
2 jumps found. (Code = 43) Position 1 = 80, Position 2 = 87
Branch analysis from position: 80
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 87
2 jumps found. (Code = 46) Position 1 = 89, Position 2 = 96
Branch analysis from position: 89
2 jumps found. (Code = 43) Position 1 = 97, Position 2 = 117
Branch analysis from position: 97
2 jumps found. (Code = 77) Position 1 = 98, Position 2 = 115
Branch analysis from position: 98
2 jumps found. (Code = 78) Position 1 = 99, Position 2 = 115
Branch analysis from position: 99
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
Branch analysis from position: 115
1 jumps found. (Code = 42) Position 1 = 124
Branch analysis from position: 124
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 115
Branch analysis from position: 117
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 96
Branch analysis from position: 126
2 jumps found. (Code = 43) Position 1 = 130, Position 2 = 139
Branch analysis from position: 130
Return found
Branch analysis from position: 139
Branch analysis from position: 126
Branch analysis from position: 127
Branch analysis from position: 37
Branch analysis from position: 39
Branch analysis from position: 70
filename:       /in/Kgnbt
function name:  convert
number of ops:  141
compiled vars:  !0 = $node_name, !1 = $arr, !2 = $xml, !3 = $node, !4 = $value, !5 = $key, !6 = $v, !7 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   76     2        INIT_STATIC_METHOD_CALL                                  'getXMLRoot'
          3        DO_FCALL                                      0  $8      
          4        ASSIGN                                                   !2, $8
   77     5        INIT_METHOD_CALL                                         !2, 'createElement'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $10     
          8        ASSIGN                                                   !3, $10
   79     9        TYPE_CHECK                                  128          !1
         10      > JMPZ                                                     ~12, ->70
   81    11    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, '%40attributes'
         12      > JMPZ                                                     ~13, ->39
   82    13    >   FETCH_DIM_R                                      ~14     !1, '%40attributes'
         14      > FE_RESET_R                                       $15     ~14, ->37
         15    > > FE_FETCH_R                                       ~16     $15, !4, ->37
         16    >   ASSIGN                                                   !5, ~16
   83    17        INIT_STATIC_METHOD_CALL                                  'isValidTagName'
         18        SEND_VAR_EX                                              !5
         19        DO_FCALL                                      0  $18     
         20        BOOL_NOT                                         ~19     $18
         21      > JMPZ                                                     ~19, ->29
   84    22    >   NEW                                              $20     'Exception'
         23        CONCAT                                           ~21     '%5BArray2XML%5D+Illegal+character+in+attribute+name.+attribute%3A+', !5
         24        CONCAT                                           ~22     ~21, '+in+node%3A+'
         25        CONCAT                                           ~23     ~22, !0
         26        SEND_VAL_EX                                              ~23
         27        DO_FCALL                                      0          
         28      > THROW                                         0          $20
   86    29    >   INIT_METHOD_CALL                                         !3, 'setAttribute'
         30        SEND_VAR_EX                                              !5
         31        INIT_STATIC_METHOD_CALL                                  'bool2str'
         32        SEND_VAR_EX                                              !4
         33        DO_FCALL                                      0  $25     
         34        SEND_VAR_NO_REF_EX                                       $25
         35        DO_FCALL                                      0          
   82    36      > JMP                                                      ->15
         37    >   FE_FREE                                                  $15
   88    38        UNSET_DIM                                                !1, '%40attributes'
   93    39    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, '%40value'
         40      > JMPZ                                                     ~27, ->55
   94    41    >   INIT_METHOD_CALL                                         !3, 'appendChild'
         42        INIT_METHOD_CALL                                         !2, 'createTextNode'
         43        INIT_STATIC_METHOD_CALL                                  'bool2str'
         44        CHECK_FUNC_ARG                                           
         45        FETCH_DIM_FUNC_ARG                               $28     !1, '%40value'
         46        SEND_FUNC_ARG                                            $28
         47        DO_FCALL                                      0  $29     
         48        SEND_VAR_NO_REF_EX                                       $29
         49        DO_FCALL                                      0  $30     
         50        SEND_VAR_NO_REF_EX                                       $30
         51        DO_FCALL                                      0          
   95    52        UNSET_DIM                                                !1, '%40value'
   97    53      > RETURN_BY_REF                                            !3
         54*       JMP                                                      ->70
   98    55    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, '%40cdata'
         56      > JMPZ                                                     ~32, ->70
   99    57    >   INIT_METHOD_CALL                                         !3, 'appendChild'
         58        INIT_METHOD_CALL                                         !2, 'createCDATASection'
         59        INIT_STATIC_METHOD_CALL                                  'bool2str'
         60        CHECK_FUNC_ARG                                           
         61        FETCH_DIM_FUNC_ARG                               $33     !1, '%40cdata'
         62        SEND_FUNC_ARG                                            $33
         63        DO_FCALL                                      0  $34     
         64        SEND_VAR_NO_REF_EX                                       $34
         65        DO_FCALL                                      0  $35     
         66        SEND_VAR_NO_REF_EX                                       $35
         67        DO_FCALL                                      0          
  100    68        UNSET_DIM                                                !1, '%40cdata'
  102    69      > RETURN_BY_REF                                            !3
  107    70    >   TYPE_CHECK                                  128          !1
         71      > JMPZ                                                     ~37, ->127
  109    72    > > FE_RESET_R                                       $38     !1, ->126
         73    > > FE_FETCH_R                                       ~39     $38, !4, ->126
         74    >   ASSIGN                                                   !5, ~39
  110    75        INIT_STATIC_METHOD_CALL                                  'isValidTagName'
         76        SEND_VAR_EX                                              !5
         77        DO_FCALL                                      0  $41     
         78        BOOL_NOT                                         ~42     $41
         79      > JMPZ                                                     ~42, ->87
  111    80    >   NEW                                              $43     'Exception'
         81        CONCAT                                           ~44     '%5BArray2XML%5D+Illegal+character+in+tag+name.+tag%3A+', !5
         82        CONCAT                                           ~45     ~44, '+in+node%3A+'
         83        CONCAT                                           ~46     ~45, !0
         84        SEND_VAL_EX                                              ~46
         85        DO_FCALL                                      0          
         86      > THROW                                         0          $43
  113    87    >   TYPE_CHECK                                  128  ~48     !4
         88      > JMPZ_EX                                          ~48     ~48, ->96
         89    >   INIT_FCALL                                               'is_numeric'
         90        INIT_FCALL                                               'key'
         91        SEND_VAR                                                 !4
         92        DO_ICALL                                         $49     
         93        SEND_VAR                                                 $49
         94        DO_ICALL                                         $50     
         95        BOOL                                             ~48     $50
         96    > > JMPZ                                                     ~48, ->117
  117    97    > > FE_RESET_R                                       $51     !4, ->115
         98    > > FE_FETCH_R                                       ~52     $51, !6, ->115
         99    >   ASSIGN                                                   !7, ~52
  118   100        INIT_METHOD_CALL                                         !3, 'appendChild'
        101        INIT_STATIC_METHOD_CALL                                  'convert'
        102        SEND_VAR                                                 !5
        103        SEND_VAL                                                 'item'
        104        DO_FCALL                                      0  $54     
        105        SEND_VAR_NO_REF_EX                                       $54
        106        DO_FCALL                                      0          
  119   107        INIT_METHOD_CALL                                         !3, 'appendChild'
        108        INIT_STATIC_METHOD_CALL                                  'convert'
        109        SEND_VAR                                                 !5
        110        SEND_VAR                                                 !6
        111        DO_FCALL                                      0  $56     
        112        SEND_VAR_NO_REF_EX                                       $56
        113        DO_FCALL                                      0          
  117   114      > JMP                                                      ->98
        115    >   FE_FREE                                                  $51
        116      > JMP                                                      ->124
  123   117    >   INIT_METHOD_CALL                                         !3, 'appendChild'
        118        INIT_STATIC_METHOD_CALL                                  'convert'
        119        SEND_VAR                                                 !5
        120        SEND_VAR                                                 !4
        121        DO_FCALL                                      0  $58     
        122        SEND_VAR_NO_REF_EX                                       $58
        123        DO_FCALL                                      0          
  125   124    >   UNSET_DIM                                                !1, !5
  109   125      > JMP                                                      ->73
        126    >   FE_FREE                                                  $38
  131   127    >   TYPE_CHECK                                  128  ~60     !1
        128        BOOL_NOT                                         ~61     ~60
        129      > JMPZ                                                     ~61, ->139
  132   130    >   INIT_METHOD_CALL                                         !3, 'appendChild'
        131        INIT_METHOD_CALL                                         !2, 'createTextNode'
        132        INIT_STATIC_METHOD_CALL                                  'bool2str'
        133        SEND_VAR_EX                                              !1
        134        DO_FCALL                                      0  $62     
        135        SEND_VAR_NO_REF_EX                                       $62
        136        DO_FCALL                                      0  $63     
        137        SEND_VAR_NO_REF_EX                                       $63
        138        DO_FCALL                                      0          
  135   139    > > RETURN_BY_REF                                            !3
  136   140*     > RETURN_BY_REF                                            null

End of function convert

Function getxmlroot:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 4
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
filename:       /in/Kgnbt
function name:  getXMLRoot
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  142     0  E >   ISSET_ISEMPTY_STATIC_PROP                                'xml'
          1      > JMPZ                                                     ~0, ->4
  143     2    >   INIT_STATIC_METHOD_CALL                                  'init'
          3        DO_FCALL                                      0          
  145     4    >   FETCH_STATIC_PROP_R          unknown             ~2      'xml'
          5      > RETURN                                                   ~2
  146     6*     > RETURN                                                   null

End of function getxmlroot

Function bool2str:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
Branch analysis from position: 11
filename:       /in/Kgnbt
function name:  bool2str
number of ops:  15
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  151     0  E >   RECV                                             !0      
  153     1        TYPE_CHECK                                    8          !0
          2      > JMPZ                                                     ~1, ->5
          3    >   QM_ASSIGN                                        ~2      'true'
          4      > JMP                                                      ->6
          5    >   QM_ASSIGN                                        ~2      !0
          6    >   ASSIGN                                                   !0, ~2
  154     7        TYPE_CHECK                                    4          !0
          8      > JMPZ                                                     ~4, ->11
          9    >   QM_ASSIGN                                        ~5      'false'
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~5      !0
         12    >   ASSIGN                                                   !0, ~5
  155    13      > RETURN                                                   !0
  156    14*     > RETURN                                                   null

End of function bool2str

Function isvalidtagname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/Kgnbt
function name:  isValidTagName
number of ops:  13
compiled vars:  !0 = $tag, !1 = $pattern, !2 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  162     0  E >   RECV                                             !0      
  163     1        ASSIGN                                                   !1, '%2F%5E%5Ba-z_%5D%2B%5Ba-z0-9%5C%3A%5C-%5C.%5C_%5D%2A%5B%5E%3A%5D%2A%24%2Fi'
  164     2        INIT_FCALL                                               'preg_match'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        SEND_REF                                                 !2
          6        DO_ICALL                                         $4      
          7      > JMPZ_EX                                          ~5      $4, ->11
          8    >   FETCH_DIM_R                                      ~6      !2, 0
          9        IS_EQUAL                                         ~7      !0, ~6
         10        BOOL                                             ~5      ~7
         11    > > RETURN                                                   ~5
  165    12*     > RETURN                                                   null

End of function isvalidtagname

End of class Array2XML.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.64 ms | 1420 KiB | 19 Q