PHP Doku:: Adds a new child at the end of the children - function.domnode-append-child.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDOM-XMLDOM-XML-FunktionenDomNode->append_child

Ein Service von Reinhard Neidl - Webprogrammierung.

DOM-XML-Funktionen

<<DomNode->add_namespace

DomNode->append_sibling>>

DomNode->append_child

(PHP 4 >= 4.1.0)

DomNode->append_child Adds a new child at the end of the children

Beschreibung

DOMNode DOMNode::append_child ( DOMNode $newnode )

This functions appends a child to an existing list of children or creates a new list of children.

Parameter-Liste

newnode

The node being appended. It can be created with e.g. DomDocument->create_element, DomDocument->create_text_node etc. or simply by using any other node.

Hinweis:

You can not append a DOMAttribute using this method. Use DomElement->set_attribute instead.

Rückgabewerte

Returns the appended node on successIm Fehlerfall wird FALSE zurückgegeben..

Changelog

Version Beschreibung
4.3.0 You are not allowed anymore to insert a node from another document.
4.3.0 Prior to PHP 4.3.0, the new child is duplicated before being appended. Therefore the new child is a completely new copy which can be modified without changing the node which was passed to this function. If the node passed has children itself, they will be duplicated as well, which makes it quite easy to duplicate large parts of an XML document. The return value is the appended child. If you plan to do further modifications on the appended child you must use the returned node.
4.3.0 and 4.3.1 The new child newnode is first unlinked from its existing context, if it's already a child of DomNode. Therefore the newnode is moved and not copies anymore. This is the behaviour according to the W3C specifications. If you need the old behaviour, use DomNode->clone_node before appending.
4.3.2 The new child newnode is first unlinked from its existing context, if it's already in the tree. Same rules apply.

Beispiele

The following example adds a new element node to a fresh document and sets the attribute align to left.

Beispiel #1 Adding a child

<?php
$doc 
domxml_new_doc("1.0");
$node $doc->create_element("para");
$newnode $doc->append_child($node);
$newnode->set_attribute("align""left");
?>

The above example could also be written as the following:

Beispiel #2 Adding a child

<?php
$doc 
domxml_new_doc("1.0");
$node $doc->create_element("para");
$node->set_attribute("align""left");
$newnode $doc->append_child($node);
?>

A more complex example is the one below. It first searches for a certain element, duplicates it including its children and adds it as a sibling. Finally a new attribute is added to one of the children of the new sibling and the whole document is dumped.

Beispiel #3 Adding a child

<?php
include("example.inc");

if (!
$dom domxml_open_mem($xmlstr)) {
  echo 
"Error while parsing the document\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);
?>

The above example could also be done with DomNode->insert_before instead of DomNode->append_child.

Migrating to PHP 5

You should use DOMNode::appendChild.


2 BenutzerBeiträge:
- Beiträge aktualisieren...
andrew at transitionmedia dot co dot uk
12.02.2006 16:03
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.
wackysalut at bourget dot cc
1.11.2002 21: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)