DomNode::append_child
(PHP 4 >= 4.1.0)
DomNode::append_child — Adds a new child at the end of the children
Описание
This functions appends a child to an existing list of children or creates a new list of children.
Список параметров
- 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.
Замечание:
You can not append a DOMAttribute using this method. Use DomElement::set_attribute instead.
Возвращаемые значения
Returns the appended node on success или FALSE в случае возникновения ошибки.
Список изменений
Версия | Описание |
---|---|
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. |
Примеры
The following example adds a new element node to a fresh document and sets the attribute align to left.
Пример #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:
Пример #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.
Пример #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.
- DomAttribute::name
- DomAttribute::set_value
- DomAttribute::specified
- DomAttribute::value
- DomDocument::add_root
- DomDocument::create_attribute
- DomDocument::create_cdata_section
- DomDocument::create_comment
- DomDocument::create_element_ns
- DomDocument::create_element
- DomDocument::create_entity_reference
- DomDocument::create_processing_instruction
- DomDocument::create_text_node
- DomDocument::doctype
- DomDocument::document_element
- DomDocument::dump_file
- DomDocument::dump_mem
- DomDocument::get_element_by_id
- DomDocument::get_elements_by_tagname
- DomDocument::html_dump_mem
- DomDocument::xinclude
- DomDocumentType::entities
- DomDocumentType::internal_subset
- DomDocumentType::name
- DomDocumentType::notations
- DomDocumentType::public_id
- DomDocumentType::system_id
- DomElement::get_attribute_node
- DomElement::get_attribute
- DomElement::get_elements_by_tagname
- DomElement::has_attribute
- DomElement::remove_attribute
- DomElement::set_attribute_node
- DomElement::set_attribute
- DomElement::tagname
- DomNode::add_namespace
- DomNode::append_child
- DomNode::append_sibling
- DomNode::attributes
- DomNode::child_nodes
- DomNode::clone_node
- DomNode::dump_node
- DomNode::first_child
- DomNode::get_content
- DomNode::has_attributes
- DomNode::has_child_nodes
- DomNode::insert_before
- DomNode::is_blank_node
- DomNode::last_child
- DomNode::next_sibling
- DomNode::node_name
- DomNode::node_type
- DomNode::node_value
- DomNode::owner_document
- DomNode::parent_node
- DomNode::prefix
- DomNode::previous_sibling
- DomNode::remove_child
- DomNode::replace_child
- DomNode::replace_node
- DomNode::set_content
- DomNode::set_name
- DomNode::set_namespace
- DomNode::unlink_node
- DomProcessingInstruction::data
- DomProcessingInstruction::target
- DomXsltStylesheet::process
- DomXsltStylesheet::result_dump_file
- DomXsltStylesheet::result_dump_mem
- domxml_new_doc
- domxml_open_file
- domxml_open_mem
- domxml_version
- domxml_xmltree
- domxml_xslt_stylesheet_doc
- domxml_xslt_stylesheet_file
- domxml_xslt_stylesheet
- domxml_xslt_version
- xpath_eval_expression
- xpath_eval
- xpath_new_context
- xpath_register_ns_auto
- xpath_register_ns
- xptr_eval
- xptr_new_context
Коментарии
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.
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.