As of version 4.3 PHP doesn't support Appending a child from another source document. If you are trying to import information from multiple sources into a final document [for transformation using XSL as an example] then you can have problems. This technique can be used to do it though.
I am assuming you have two documents open, $xmldoc1 and $xmldoc2 and you have selected [via XPath or explicit searching] the nodes you want in each document. Thus $xmldoc1_appending_node is the node you would like to add $xmldoc2_importnode to.
<?
// first we create a temporary node within the document we want to add to.
$temp_importnode = $xmldoc1->create_element("import");
// now we have a node that is in the right document context we can clone the one we want into this one.
$temp_importnode = $xmldoc_importnode->clone_node(true);
// by using true in the above call to clone_node() we copy all of the child nodes as well. Use false or nothing if you just want the containing node with no children.
$xmldoc1_appending->append_child($importnode);
?>
Now your document contains the new nodes imported from a different document.
DomNode::append_child
(PHP 4 >= 4.1.0)
DomNode::append_child — Düğüme en küçük çocuk olarak bir çocuk ekler
Açıklama
Bu işlev mevcut çocukların sonuna bir çocuk daha ekler. Eğer düğümün hiç çocuğu yoksa yeni bir çocuk listesini oluşturulur.
Değiştirgeler
- yeniDüğüm
-
Eklenecek düğüm; DomDocument::create_element, DomDocument::create-text-node gibi işlevlerle oluşturulabilir veya başka bir düğüm kullanılabililir.
Bilginize: Bu yöntemi kullanarak düğüme yeni bir bir öznitelik ekleyemezsiniz. Bunun için DomElement::set_attribute yöntemini kullanmalısınız.
Dönen Değerler
İşlem başarılı olursa eklenen düğüm, olmazsa FALSE döner.
Sürüm Bilgisi
| Sürüm: | Açıklama |
|---|---|
| 4.3.0 | Başka bir belgenin düğümünün eklenmesine artık izin verilmiyor. |
| 4.3.0 | PHP 4.3.0 öncesinde, yeni çocuk eklenmeden önce bir kopyası yapılırdı. Böylece işleve aktarılan düğüme dokunulmadan kopyası üzerinde değişiklik yapılabilirdi. Aktarılan düğümün kendi çocukları varsa onlar da kopyalanırdı. Böylece bir XML belgeyi büyük parçalar halinde kopyalamak kolay olurdu. Yöntemin dönüş değeri eklenen düğüm olurdu. Eklenen düğüm üzerinde değişiklik yapmak için bu dönüş değerini kullanmak gerekirdi (işleve aktarılanı değil). |
| 4.3.0 ve 4.3.1 | Yeni düğüm eklenmeden önce, ekleneceği bağlamda zaten mevcutsa eski düğüm silinir. Böylece yeni düğüm taşınırken artık bir kopyasının yapılmasına gerek kalmaz. Bu davranış W3C DOM belirtimine uygundur. Eski davranışı tercih ediyorsanız bu yöntemi çağırmadan önce bir DomNode::clone_node çağrısı yapmalısınız. |
| 4.3.2 | Yeni düğüm eklenmeden önce, ekleneceği ağaçta zaten mevcutsa eski düğüm silinir. PHP 4.3.1 sürümündeki davranış uygulanır. |
Örnekler
Aşağıdaki örnekte yeni bir belgeye bir eleman düğümü eklendikten sonra elemanın align özniteliğine left değeri atanmaktadır.
Örnek 1 - Bir çocuk düğüm eklemek
<?php
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("para");
$newnode = $doc->append_child($node);
$newnode->set_attribute("align", "left");
?>
Yukarıdaki örnek şöyle de yazılabilirdi:
Örnek 2 - Bir çocuk eklemek
<?php
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("para");
$node->set_attribute("align", "left");
$newnode = $doc->append_child($node);
?>
Aşağıda daha karmaşık bir örneğe yer verilmiştir. Önce belli bir eleman aranmakta, sonra bu eleman çocukları ile birlikte bir kopyası yapılıp bir kardeş düğüm olarak eklenmektedir. Son olarak eklenen elemana bir öznitelik atanmakta ve tüm belge çıktılanmaktadır.
Örnek 3 - Bir düğümü kendine kardeş olarak eklemek
<?php
include("example.inc");
if (!$dom = domxml_open_mem($xmlstr)) {
echo "Belge çözümlenirken hata oluştu\n";
exit;
}
$elements = $dom->get_elements_by_tagname("informaltable");
print_r($elements);
$element = $elements[0];
$parent = $element->parent_node();
$newnode = $parent->append_child($element);
$children = $newnode->children();
$attr = $children[1]->set_attribute("align", "left");
$xmlfile = $dom->dump_mem();
echo htmlentities($xmlfile);
?>
Yukarıdaki örnek DomNode::append-child yerine DomNode::insert_before kullanarak da yazılabilirdi.
PHP 5'e yükseltme
DomNode::appendChild yöntemini kullanın.
Ayrıca Bakınız
- DomNode::insert_before - Düğümün öncesine yeni bir düğüm yerleştirir
- DomNode::clone_node - Düğümün yeni bir kopyasını yapar
DomNode::append_child
12-Feb-2006 03:03
01-Nov-2002 08:46
Be careful of the order you append childs. (PHP <= 4.2)
If you create let's say:
$x_html = $x_doc->create_element('html');
$x_head, $x_title, $x_meta, $x_link and $x_body in the same way.
If you:
$x_head->append_child($x_title);
$x_head->append_child($x_meta);
$x_head->append_child($x_link);
$x_html->append_child($x_head);
$x_body_backup = $x_html->append_child($x_body);
/* here */
$x_doc->append_child($x_html);
If you now try to modify $x_body_backup.. it will no longer be able to modify the actualy XML tree, because you appended the HTML tree to the DomDocument.. and it has copied ALL the nodes at the end of the DomDocument, so the reference you now have in $x_body_backup is not valid anymore! If you had modified it where the /* here */ line is.. the $x_body_backup would still be valid.. but not after the $x_doc->append_child($x_html);
What you can do is, reverse the order of your appendings.. so that you append the highest level node at the end.. so that your reference is still valid after, or wait till you have filled all of the nodes you wanted to add before you append it to your parent node.
