PHP Conference Nagoya 2025

DOMDocument::adoptNode

(PHP >= 8.3)

DOMDocument::adoptNodeTransfer a node from another document

Açıklama

public DOMDocument::adoptNode(DOMNode $node): DOMNode|false

Transfer a node from another document into the current document.

Bağımsız Değişkenler

node

The node to transfer.

Dönen Değerler

The node that was transfered, or false on error.

Hatalar/İstisnalar

DOM_NOT_SUPPORTED_ERR

Raised if the node type is not supported for document transfers.

Örnekler

Örnek 1 DOMDocument::adoptNode() example

Transfers the hello element from the first document to the second one.

<?php
$doc1
= new DOMDocument;
$doc1->loadXML("<container><hello><world/></hello></container>");
$hello = $doc1->documentElement->firstChild;

$doc2 = new DOMDocument;
$doc2->loadXML("<root/>");
$doc2->documentElement->appendChild($doc2->adoptNode($hello));

echo
$doc1->saveXML() . PHP_EOL;
echo
$doc2->saveXML();
?>

Yukarıdaki örneğin çıktısı:

<?xml version="1.0"?>
<container/>

<?xml version="1.0"?>
<root><hello><world/></hello></root>

Ayrıca Bakınız

add a note

User Contributed Notes 1 note

up
0
Anonymous
1 month ago
Note that this function will remove the node from the original XML document
To Top