note 75712 added to function.dom-domdocument-createelement

From: Date: Tue, 12 Jun 2007 21:38:27 +0000
Subject: note 75712 added to function.dom-domdocument-createelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-127431@lists.php.net to get a copy of this message
Note that the second parameter (value), although convenient, is non-standard. You should create elements like this instead: <?php $doc = new DOMDocument('1.0', 'iso-8859-1'); $root = $doc->createElement('test'); $doc->appendChild($root); $root_text = $doc->createTextNode('This is the root element!'); $root->appendChild($root_text); print $doc->saveXML(); ?> Or, alternatively, extend the DOMDocument class and add your own custom, convenience method to avoid intruding on the standard: <?php class CustomDOMDocument extends DOMDocument { function createElementWithText($name, $child_text) { // Creates an element with a child text node // @param string $name element tag name // @param string $child_text child node text // @return object new element $element = $this->createElement($name); $element_text = $this->createTextNode($child_text); $element->appendChild($element_text); return $element; } } $doc = new CustomDOMDocument('1.0', 'iso-8859-1'); $root = $doc->createElementWithText('test', 'This is the root element!'); $doc->appendChild($root); print $doc->saveXML(); ?> Also use caution with (or avoid) the 'DOMElement->nodeValue' property. It can return some unexpected values and changing its value will replace (remove) all descendants of the element with a single text node. It's also non-standard; according to the DOM spec it should return NULL. ---- Server IP: 69.147.83.197 Probable Submitter: 208.117.7.211 ---- Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-createelement.php Edit -- https://master.php.net/note/edit/75712 Del: integrated -- https://master.php.net/note/delete/75712/integrated Del: useless -- https://master.php.net/note/delete/75712/useless Del: bad code -- https://master.php.net/note/delete/75712/bad+code Del: spam -- https://master.php.net/note/delete/75712/spam Del: non-english -- https://master.php.net/note/delete/75712/non-english Del: in docs -- https://master.php.net/note/delete/75712/in+docs Del: other reasons-- https://master.php.net/note/delete/75712 Reject -- https://master.php.net/note/reject/75712 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#127431) next »