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 */ /** * 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(); $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/OfZPL
function name:  (null)
number of ops:  54
compiled vars:  !0 = $parent, !1 = $before, !2 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   NEW                                              $3      'SimpleXMLElement'
          1        SEND_VAL_EX                                              '%3Cparent%2F%3E'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
   39     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          
   43     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          
   46    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          
   53    21        INIT_FCALL                                               'simplexml_load_string'
         22        INIT_METHOD_CALL                                         !0, 'asXML'
         23        DO_FCALL                                      0  $12     
         24        SEND_VAR                                                 $12
         25        DO_ICALL                                         $13     
         26        ASSIGN                                                   !2, $13
   91    27        INIT_FCALL                                               'simplexml_import_simplexml'
         28        SEND_VAR                                                 !0
         29        SEND_VAR                                                 !0
         30        DO_FCALL                                      0          
   94    31        INIT_FCALL                                               'simplexml_import_simplexml'
         32        INIT_METHOD_CALL                                         !0, 'children'
         33        DO_FCALL                                      0  $16     
         34        SEND_VAR                                                 $16
         35        FETCH_OBJ_R                                      ~17     !0, 'test'
         36        FETCH_OBJ_R                                      ~18     ~17, 'this'
         37        SEND_VAL                                                 ~18
         38        SEND_VAL                                                 <true>
         39        DO_FCALL                                      0          
   97    40        NEW                                              $20     'SimpleXMLElement'
         41        SEND_VAL_EX                                              '%3Ctest+attribute%3D%22value%22+%2F%3E'
         42        DO_FCALL                                      0          
         43        ASSIGN                                                   !2, $20
   98    44        INIT_FCALL                                               'simplexml_import_simplexml'
         45        SEND_VAR                                                 !0
         46        INIT_METHOD_CALL                                         !2, 'attributes'
         47        DO_FCALL                                      0  $23     
         48        SEND_VAR                                                 $23
         49        DO_FCALL                                      0          
  100    50        INIT_METHOD_CALL                                         !0, 'asXML'
         51        DO_FCALL                                      0  $25     
         52        ECHO                                                     $25
         53      > 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/OfZPL
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
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   17     3        CAST                                          6  ~6      !1
          4        ASSIGN                                                   !1, ~6
   20     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
   21    13    > > RETURN                                                   !3
   25    14    >   INIT_FCALL                                               'dom_import_simplexml'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $13     
         17        ASSIGN                                                   !4, $13
   26    18        FETCH_OBJ_R                                      ~15     !4, 'ownerDocument'
         19        INIT_METHOD_CALL                                         ~15, 'createDocumentFragment'
         20        DO_FCALL                                      0  $16     
         21        ASSIGN                                                   !5, $16
   27    22        INIT_METHOD_CALL                                         !5, 'appendXML'
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0          
   29    25      > JMPZ                                                     !2, ->33
   30    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
   33    33    >   INIT_METHOD_CALL                                         !4, 'appendChild'
         34        SEND_VAR_EX                                              !5
         35        DO_FCALL                                      0  $22     
         36        BOOL                                             ~23     $22
         37      > RETURN                                                   ~23
   34    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/OfZPL
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
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   66     3        FETCH_DIM_R                                      ~5      !1, 0
          4        IS_EQUAL                                                 ~5, null
          5      > JMPZ                                                     ~6, ->7
   67     6    > > RETURN                                                   <true>
   71     7    >   FETCH_DIM_R                                      ~7      !1, 0
          8        ASSIGN                                                   !1, ~7
   74     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
   75    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
   76    20      > RETURN                                                   <true>
   79    21    >   INIT_METHOD_CALL                                         !1, 'asXML'
         22        DO_FCALL                                      0  $15     
         23        ASSIGN                                                   !3, $15
   82    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
   83    30    >   INIT_FCALL                                               'strpos'
         31        SEND_VAR                                                 !3
         32        SEND_VAL                                                 '%0A'
         33        DO_ICALL                                         $20     
         34        ASSIGN                                                   !4, $20
   84    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
   87    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
   88    47*     > RETURN                                                   null

End of function simplexml_import_simplexml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.18 ms | 1411 KiB | 28 Q