3v4l.org - online PHP & HHVM shell, execute code in 100+ different versions!

<?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();
Output for 5.2.2 - 5.6.0, php7@20140507 - 20140901, hhvm-3.0.1 - 3.2.0
<?xml version="1.0"?> <!-- this works, too --> <parent> <!-- leave a comment --> <test><this>now</this></test> </parent> ......................................... <?xml version="1.0"?> <!-- this works, too --> <parent attribute="value"> <!-- leave a comment --> <this>now</this><test><this>now</this></test> <!-- this works, too --> <parent> <!-- leave a comment --> <test><this>now</this></test> </parent> </parent>
Output for 5.2.0 - 5.2.1
<?xml version="1.0"?> <parent/> ......................................... Catchable fatal error: Argument 2 passed to simplexml_import_simplexml() must be an instance of SimpleXMLElement, null given, called in /in/XqmEC on line 95 and defined in /in/XqmEC on line 64
Process exited with code 255.
Output for 5.1.0 - 5.1.6
<?xml version="1.0"?> <parent/> ......................................... Fatal error: Argument 2 passed to simplexml_import_simplexml() must not be null, called in /in/XqmEC on line 95 and defined in /in/XqmEC on line 64
Process exited with code 255.
Output for 5.0.0 - 5.0.5
<?xml version="1.0"?> <parent/> ......................................... Fatal error: Argument 2 must not be null in /in/XqmEC on line 64
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/XqmEC on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting ')' in /in/XqmEC on line 16
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/XqmEC on line 16
Process exited with code 255.