PHP 8.5.4 Released!

DOMDocument::importNode

(PHP 5, PHP 7, PHP 8)

DOMDocument::importNode現在のドキュメントにノードをインポートする

説明

public DOMDocument::importNode(DOMNode $node, bool $deep = false): DOMNode|false

この関数は、インポートするノードのコピーを返し、 それを現在のドキュメントに関連付けます。

パラメータ

node

インポートするノード。

deep

true の場合、このメソッドは node 以下のサブツリーも再帰的にインポートします。

注意:

ノードの属性をコピーするには deeptrue に設定しなければなりません。

戻り値

コピーされたノードを返します。 コピーできなかった場合は false を返します。

エラー / 例外

ノードがインポートできなかった場合には DOMException がスローされます。

例1 DOMDocument::importNode() の例

ノードをドキュメント間でコピーします。

<?php

$orgdoc
= new DOMDocument;
$orgdoc->loadXML("<root><element><child>text in child</child></element></root>");

// 新しいドキュメントにインポートしたいノード
$node = $orgdoc->getElementsByTagName("element")->item(0);


// 新しいドキュメントを作成します
$newdoc = new DOMDocument;
$newdoc->formatOutput = true;

// マークアップを追加します
$newdoc->loadXML("<root><someelement>text in some element</someelement></root>");

echo
"The 'new document' before copying nodes into it:\n";
echo
$newdoc->saveXML();

// ノードとそのすべての子をドキュメントにインポートします
$node = $newdoc->importNode($node, true);
// それから、それを "<root>" ノードに追加します
$newdoc->documentElement->appendChild($node);

echo
"\nThe 'new document' after copying the nodes into it:\n";
echo
$newdoc->saveXML();
?>

上の例の出力は以下となります。

The 'new document' before copying nodes into it:
<?xml version="1.0"?>
<root>
  <someelement>text in some element</someelement>
</root>

The 'new document' after copying the nodes into it:
<?xml version="1.0"?>
<root>
  <someelement>text in some element</someelement>
  <element>
    <child>text in child</child>
  </element>
</root>

add a note

User Contributed Notes 2 notes

up
4
c dot 1 at smithies dot org
16 years ago
Assume that $source and $dest are instances of DOMDocument. Assume that $sourcedoc contains an element with ID 'sourceID' and that $destdoc contains an element with ID 'destID'. Assume that we have already set up source and destination element variables thus:

<?php
 $src = $sourcedoc->getElementById('sourceID');
 $dst = $destdoc->getElementById('destID');
?>

Finally, assume that $sel has more than one child node.

In order to import the child elements of $src as children of $dst, you might do something like this:

<?php
$src = $dest->importNode($src, TRUE);

foreach ($src->childNodes as $el) $dst->appendChild($el);
?>

But this does not work. foreach gets confused, because (as noted below) appending an imported element to another existing element in the same document causes it to be removed from its current parent element.

Therefore, the following technique should be used:

<?php
foreach ($src->childNodes as $el) $dst->appendChild($destdoc->importNode($el, TRUE));
?>
up
3
Fitopaldi
20 years ago
importNode returns a copy of the node to import and associates it with the current document, but not import the node to the current DOMDocument. Use appendChild for import the copy of the node to current DOMDocument.

<?
 $domNode = $dom->importNode($aDomNode, true);
 $currentDomDocument->appendChild($domNode);
?>
To Top