PHP 8.5.4 Released!

DOMDocument::importNode

(PHP 5, PHP 7, PHP 8)

DOMDocument::importNodeBelgeye bir düğüm dahil eder

Açıklama

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

Dahil edilen düğümü belge ile ilişkilendirip düğümün bir kopyasını döndürür.

Bağımsız Değişkenler

node

Belgeye dahil edilecek düğüm.

deep

true belirtiliği takdirde node alt düğümleriyle birlikte belgeye dahil edilir.

Bilginize:

Düğümlerin özniteliklerini de kopyalamak için bu bağımsız değişkende true belirtmek gerekir.

Dönen Değerler

Düğüm kopyalanamazsa false, yoksa kopyalanan düğümü döndürür.

Hatalar/İstisnalar

Düğüm dahil edilemezse DOMException istisnası oluşur.

Örnekler

Örnek 1 - DOMDocument::importNode() örneği

Düğümlerin bir belgeden başka bir belgeye kopyalanması.

<?php

$orgdoc
= new DOMDocument;
$orgdoc->loadXML("<root><eleman><çocuk>çocuk metin</çocuk></eleman></root>");

// Yeni belgeye dahil edilecek düğüm
$node = $orgdoc->getElementsByTagName("element")->item(0);


// Yeni belgeyi oluşturalım
$newdoc = new DOMDocument;
$newdoc->formatOutput = true;

// Biraz imlenim ekleyelim
$newdoc->loadXML("<belge><bireleman>elemanın içeriği</bireleman></belge>");

echo
"Düğümler kopyalanmadan 'yeni belge':\n";
echo
$newdoc->saveXML();

// Düğümü alt düğümleriyle birlikte belgeye dahil edelim
$node = $newdoc->importNode($node, true);
// ve "<belge>" düğümüne ekleyelim
$newdoc->documentElement->appendChild($node);

echo
"\nDüğümler kopyalandıktan sonra 'yeni belge':\n";
echo
$newdoc->saveXML();
?>

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

Düğümler kopyalanmadan 'yeni belge':
<?xml version="1.0" encoding="utf-8"?>
<belge>
  <bireleman>elemanın içeriği</bireleman>
</belge>

Düğümler kopyalandıktan sonra 'yeni belge':
<?xml version="1.0" encoding="utf-8"?>
<belge>
  <bireleman>elemanın içeriği</bireleman>
  <eleman>
    <çocuk>çocuk metin</çocuk>
  </eleman>
</belge>

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