Tuesday, June 19, 2007

PHP Magic: Watch carefully as I place this DOMNode into a hat

Nab this, save it somewhere (I did it with 5.2.3). Spot my magic trick, and X_import_Y not doing what you'd assume.

<?php
$sxeA = simplexml_load_string('<nowYouSeeIt><nowYouDont /></nowYouSeeIt>');
$root = dom_import_simplexml($sxeA);
$sxeB = simplexml_import_dom($root);

print "Simplexml A\n";
print $sxeA->asXML() . "\n";

print "Simplexml B\n";
print $sxeB->asXML() . "\n";

print "I simply utter the magic xpath phrases...\n\n";

$items = $sxeA->xpath("//nowYouDont");
$node = dom_import_simplexml($items[0]);
$node->parentNode->removeChild($node);

print "ABBRA-KA-FREAKIN'-DABRA!\n";

print "Simplexml A\n";
print $sxeA->asXML() . "\n";

print "Simplexml B\n";
print $sxeB->asXML() . "\n";

No comments: