PHP Doku:: Create new element node - function.domdocument-create-element.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:

Ein Service von Reinhard Neidl - Webprogrammierung.





(PHP 4 >= 4.1.0)

DomDocument->create_elementCreate new element node


domelement DomDocument->create_element ( string $name )

This function returns a new instance of class DomElement. The tag name of the element is the value of the passed parameter. Dieser Knoten wird in Ihrem Dokument nicht sichtbar sein, bis dieser zum Beispiel mit der Funktion domnode_append_child() eingefügt wird.

The return value is FALSE if an error occurred.

See also domdocument_create_element_ns(), domnode_append_child(), domdocument_create_text(), domdocument_create_comment(), domdocument_create_attribute(), domdocument_create_processing_instruction(), domdocument_create_entity_reference(), and domnode_insert_before().

6 BenutzerBeiträge:
- Beiträge aktualisieren...
arimbourg at ariworld dot eu
16.03.2009 22:59
It seem that the domDocument 'create_element' method don't work anymore in php 5.2.6.
We have to use the DOM 'createElement' instead.
Mikael Ljungberg
3.05.2005 19:46
When creating empty nodes like xhtml:link, the output misses the closing slash. This is what I got:

<link ...>
25.11.2003 8:58
The first example on this page is also a bit verbose. Since the DomElement class inherits/extends the DomNode class, one does not, in fact, have to get another reference to the first node. The create_element method has already given you that reference. (Also, the last line passes a string to the append_child method, instead of a DomNode object) Simplified:

= $dom->create_element("new_element");
Jorrit Kronjee
5.11.2003 16:15
The example done by Nico Almhoedi has a small error.

Since append_child only accepts DomElement (which is an object), it should've been:

$new_element = DomDocument->create_element("new_el");
$new_node = $existent_node->append_child($new_element);
Nico almhoedi at gmx dot de
5.01.2003 0:40
It's easier possible since DomNode->append_child($new_element) returns the new child as an object, so you can refer to it immediately.

To append a child to the node $existent_node:

$new_element = DomDocument->create_element("new_el");
$new_node = $existent_node->append_child("new_el");

see Manual DomNode->append_child()
14.11.2002 2:27
Its important to remember, when adding a created element to an existing node using append_child(), that if you want to then add another element to that node you have to go back and set up a DOMNode instance using DOMDocument_get_elements_by_tagname or similar before you can append another node onto it.


= $dom->create_element("new_element");
//Go back and set up new element as DOMNode
$new_elementArray = $dom->get_elements_by_tagname("new_element");
$new_elementTag = $new_elementArray[0];//(if theres more than one use a for loop)


PHP Powered Diese Seite bei
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",...)