3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * In SimpleXML, how can I add an existing SimpleXMLElement as a child element? * @link http://stackoverflow.com/q/767327/367456 * @link */ /** * Insert XML into a SimpleXMLElement * * @param SimpleXMLElement $parent * @param string $xml * @param bool $before * @return bool XML string added */ function simplexml_import_xml(SimpleXMLElement $parent, $xml, $before = false) { $xml = (string)$xml; // check if there is something to add if ($nodata = !strlen($xml) or $parent[0] == NULL) { return $nodata; } // add the XML $node = dom_import_simplexml($parent); $fragment = $node->ownerDocument->createDocumentFragment(); $fragment->appendXML($xml); if ($before) { return (bool)$node->parentNode->insertBefore($fragment, $node); } return (bool)$node->appendChild($fragment); } $parent = new SimpleXMLElement('<parent/>'); // insert some XML simplexml_import_xml($parent, "\n <test><this>now</this></test>\n"); // insert some XML before a certain element, here the first <test> element // that was just added simplexml_import_xml($parent->test, "<!-- leave a comment -->\n ", $before = true); // you can place comments above the root element simplexml_import_xml($parent, "<!-- this works, too -->", $before = true); // but take care, you can produce invalid XML, too: // simplexml_add_xml($parent, "<warn><but>take care!</but> you can produce invalid XML, too</warn>", $before = true); echo $parent->asXML(), "\n.........................................\n\n"; $test = simplexml_load_string($parent->asXML()); // validate /** * Insert SimpleXMLElement into SimpleXMLElement * * @param SimpleXMLElement $parent * @param SimpleXMLElement $child * @param bool $before * @return bool SimpleXMLElement added */ function simplexml_import_simplexml(SimpleXMLElement $parent, SimpleXMLElement $child, $before = false) { // check if there is something to add if ($child[0] == NULL) { return true; } // if it is a list of SimpleXMLElements default to the first one $child = $child[0]; // insert attribute if ($child->xpath('.') != array($child)) { $parent[$child->getName()] = (string)$child; return true; } $xml = $child->asXML(); // remove the XML declaration on document elements if ($child->xpath('/*') == array($child)) { $pos = strpos($xml, "\n"); $xml = substr($xml, $pos + 1); } return simplexml_import_xml($parent, $xml, $before); } // append the element itself to itself simplexml_import_simplexml($parent, $parent); // insert <this> before the first child element (<test>) simplexml_import_simplexml($parent->children(), $parent->test->this, true); // add an attribute to the document element $test = new SimpleXMLElement('<test attribute="value" />'); simplexml_import_simplexml($parent, $test->attributes()); echo $parent->asXML();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XqmEC
function name:  (null)
number of ops:  58
compiled vars:  !0 = $parent, !1 = $before, !2 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   NEW                                              $3      'SimpleXMLElement'
          1        SEND_VAL_EX                                              '%3Cparent%2F%3E'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
   40     4        INIT_FCALL                                               'simplexml_import_xml'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 '%0A++%3Ctest%3E%3Cthis%3Enow%3C%2Fthis%3E%3C%2Ftest%3E%0A'
          7        DO_FCALL                                      0          
   44     8        INIT_FCALL                                               'simplexml_import_xml'
          9        FETCH_OBJ_R                                      ~7      !0, 'test'
         10        SEND_VAL                                                 ~7
         11        SEND_VAL                                                 '%3C%21--+leave+a+comment+--%3E%0A++'
         12        ASSIGN                                           ~8      !1, <true>
         13        SEND_VAL                                                 ~8
         14        DO_FCALL                                      0          
   47    15        INIT_FCALL                                               'simplexml_import_xml'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 '%3C%21--+this+works%2C+too+--%3E'
         18        ASSIGN                                           ~10     !1, <true>
         19        SEND_VAL                                                 ~10
         20        DO_FCALL                                      0          
   52    21        INIT_METHOD_CALL                                         !0, 'asXML'
         22        DO_FCALL                                      0  $12     
         23        ECHO                                                     $12
         24        ECHO                                                     '%0A.........................................%0A%0A'
   54    25        INIT_FCALL                                               'simplexml_load_string'
         26        INIT_METHOD_CALL                                         !0, 'asXML'
         27        DO_FCALL                                      0  $13     
         28        SEND_VAR                                                 $13
         29        DO_ICALL                                         $14     
         30        ASSIGN                                                   !2, $14
   92    31        INIT_FCALL                                               'simplexml_import_simplexml'
         32        SEND_VAR                                                 !0
         33        SEND_VAR                                                 !0
         34        DO_FCALL                                      0          
   95    35        INIT_FCALL                                               'simplexml_import_simplexml'
         36        INIT_METHOD_CALL                                         !0, 'children'
         37        DO_FCALL                                      0  $17     
         38        SEND_VAR                                                 $17
         39        FETCH_OBJ_R                                      ~18     !0, 'test'
         40        FETCH_OBJ_R                                      ~19     ~18, 'this'
         41        SEND_VAL                                                 ~19
         42        SEND_VAL                                                 <true>
         43        DO_FCALL                                      0          
   98    44        NEW                                              $21     'SimpleXMLElement'
         45        SEND_VAL_EX                                              '%3Ctest+attribute%3D%22value%22+%2F%3E'
         46        DO_FCALL                                      0          
         47        ASSIGN                                                   !2, $21
   99    48        INIT_FCALL                                               'simplexml_import_simplexml'
         49        SEND_VAR                                                 !0
         50        INIT_METHOD_CALL                                         !2, 'attributes'
         51        DO_FCALL                                      0  $24     
         52        SEND_VAR                                                 $24
         53        DO_FCALL                                      0          
  101    54        INIT_METHOD_CALL                                         !0, 'asXML'
         55        DO_FCALL                                      0  $26     
         56        ECHO                                                     $26
         57      > RETURN                                                   1

Function simplexml_import_xml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 33
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/XqmEC
function name:  simplexml_import_xml
number of ops:  39
compiled vars:  !0 = $parent, !1 = $xml, !2 = $before, !3 = $nodata, !4 = $node, !5 = $fragment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   18     3        CAST                                          6  ~6      !1
          4        ASSIGN                                                   !1, ~6
   21     5        STRLEN                                           ~8      !1
          6        BOOL_NOT                                         ~9      ~8
          7        ASSIGN                                           ~10     !3, ~9
          8      > JMPNZ_EX                                         ~10     ~10, ->12
          9    >   FETCH_DIM_R                                      ~11     !0, 0
         10        IS_EQUAL                                         ~12     ~11, null
         11        BOOL                                             ~10     ~12
         12    > > JMPZ                                                     ~10, ->14
   22    13    > > RETURN                                                   !3
   26    14    >   INIT_FCALL                                               'dom_import_simplexml'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $13     
         17        ASSIGN                                                   !4, $13
   27    18        FETCH_OBJ_R                                      ~15     !4, 'ownerDocument'
         19        INIT_METHOD_CALL                                         ~15, 'createDocumentFragment'
         20        DO_FCALL                                      0  $16     
         21        ASSIGN                                                   !5, $16
   28    22        INIT_METHOD_CALL                                         !5, 'appendXML'
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0          
   30    25      > JMPZ                                                     !2, ->33
   31    26    >   FETCH_OBJ_R                                      ~19     !4, 'parentNode'
         27        INIT_METHOD_CALL                                         ~19, 'insertBefore'
         28        SEND_VAR_EX                                              !5
         29        SEND_VAR_EX                                              !4
         30        DO_FCALL                                      0  $20     
         31        BOOL                                             ~21     $20
         32      > RETURN                                                   ~21
   34    33    >   INIT_METHOD_CALL                                         !4, 'appendChild'
         34        SEND_VAR_EX                                              !5
         35        DO_FCALL                                      0  $22     
         36        BOOL                                             ~23     $22
         37      > RETURN                                                   ~23
   35    38*     > RETURN                                                   null

End of function simplexml_import_xml

Function simplexml_import_simplexml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 41
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
filename:       /in/XqmEC
function name:  simplexml_import_simplexml
number of ops:  48
compiled vars:  !0 = $parent, !1 = $child, !2 = $before, !3 = $xml, !4 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   67     3        FETCH_DIM_R                                      ~5      !1, 0
          4        IS_EQUAL                                                 ~5, null
          5      > JMPZ                                                     ~6, ->7
   68     6    > > RETURN                                                   <true>
   72     7    >   FETCH_DIM_R                                      ~7      !1, 0
          8        ASSIGN                                                   !1, ~7
   75     9        INIT_METHOD_CALL                                         !1, 'xpath'
         10        SEND_VAL_EX                                              '.'
         11        DO_FCALL                                      0  $9      
         12        INIT_ARRAY                                       ~10     !1
         13        IS_NOT_EQUAL                                             $9, ~10
         14      > JMPZ                                                     ~11, ->21
   76    15    >   INIT_METHOD_CALL                                         !1, 'getName'
         16        DO_FCALL                                      0  $12     
         17        CAST                                          6  ~14     !1
         18        ASSIGN_DIM                                               !0, $12
         19        OP_DATA                                                  ~14
   77    20      > RETURN                                                   <true>
   80    21    >   INIT_METHOD_CALL                                         !1, 'asXML'
         22        DO_FCALL                                      0  $15     
         23        ASSIGN                                                   !3, $15
   83    24        INIT_METHOD_CALL                                         !1, 'xpath'
         25        SEND_VAL_EX                                              '%2F%2A'
         26        DO_FCALL                                      0  $17     
         27        INIT_ARRAY                                       ~18     !1
         28        IS_EQUAL                                                 $17, ~18
         29      > JMPZ                                                     ~19, ->41
   84    30    >   INIT_FCALL                                               'strpos'
         31        SEND_VAR                                                 !3
         32        SEND_VAL                                                 '%0A'
         33        DO_ICALL                                         $20     
         34        ASSIGN                                                   !4, $20
   85    35        INIT_FCALL                                               'substr'
         36        SEND_VAR                                                 !3
         37        ADD                                              ~22     !4, 1
         38        SEND_VAL                                                 ~22
         39        DO_ICALL                                         $23     
         40        ASSIGN                                                   !3, $23
   88    41    >   INIT_FCALL                                               'simplexml_import_xml'
         42        SEND_VAR                                                 !0
         43        SEND_VAR                                                 !3
         44        SEND_VAR                                                 !2
         45        DO_FCALL                                      0  $25     
         46      > RETURN                                                   $25
   89    47*     > RETURN                                                   null

End of function simplexml_import_simplexml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.81 ms | 1455 KiB | 24 Q