PHP 8.5.0 Alpha 4 available for testing

Voting

: five plus four?
(Example: nine)

The Note You're Voting On

p dot reisinger at gmail dot com
16 years ago
method (can be used as a function as well) that joins two xml files. first argument is parent xml (the one to be inserted into), second child (the one to be inserted) and third is optional argument that specifies the parent's tag where to insert children xml. If not specified then children is inserted as the last element, just before end of the root
<?php
protected function joinXML($parent, $child, $tag = null)
{
$DOMChild = new DOMDocument;
$DOMChild->loadXML($child);
$node = $DOMChild->documentElement;

$DOMParent = new DOMDocument;
$DOMParent->formatOutput = true;
$DOMParent->loadXML($parent);

$node = $DOMParent->importNode($node, true);

if (
$tag !== null) {
$tag = $DOMParent->getElementsByTagName($tag)->item(0);
$tag->appendChild($node);
} else {
$DOMParent->documentElement->appendChild($node);
}

return
$DOMParent->saveXML();
}
?>

<< Back to user notes page

To Top