$doc = new DOMDocument(); $doc->loadXML(''); // Find the first item node $item = $doc->getElementsByTagName('item')->item(0); // Clone the item node, including its attributes $clone = $item->cloneNode(true); // Add the clone to the end of the document $doc->documentElement->appendChild($clone); // Output the modified XML echo $doc->saveXML(); - Hello
- World
- Hello
- World
- Hello
$doc = new DOMDocument(); $doc->loadXML(''); // Find all the item nodes $items = $doc->getElementsByTagName('item'); // Iterate over the item nodes foreach ($items as $item) { // Clone the item node (excluding its attributes) $clone = $item->cloneNode(false); // Set the node value to uppercase $clone->nodeValue = strtoupper($item->nodeValue); // Replace the original node with the clone $item->parentNode->replaceChild($clone, $item); } // Output the modified XML echo $doc->saveXML(); - Hello
- World
It's worth noting that cloneNode is a built-in method of the DOM extension in PHP, so it doesn't require any external package or library.- HELLO
- WORLD