Re: DOMElement::setAttribute() manual example question
| From: | Andreas Korthaus | Date: | Sun, 05 Mar 2006 16:55:23 +0000 |
| Subject: | Re: DOMElement::setAttribute() manual example question | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-231477@lists.php.net to get a copy of this message | ||
Hi Rob!
Rob wrote:
Thank you very much for confirming that! I was not sure if it's really the same.<?php $doc = new DOMDocument("1.0"); $node = $doc->createElement("root"); $node->setAttribute("align", "left"); $doc->appendChild($node); echo $doc->saveXML(); ?>Both ways are perfectly valid. $node and $newnode refer to the same object. It was written the 1st way to demonstrate the return value of appendChild(), because in many cases people create the element differently. i.e. $newnode = $doc->appendChild($doc->createElement("root")); or $newnode = $doc->appendChild(new DOMElement("root"));
Also, in the event $node is created using the new DOMElement syntax: $node = new DOMElement("root"); the node is read only and must be appended into the tree before it can be modified, so the example just tries to be neutral here regarding the syntax used.$node = new DOMElement("root"); is read only, while $node = $doc->createElement("root"); is not? Why? btw., I think this: http://news.php.net/php.internals/22117 is a very, very, very good idea! I hope the patch will make it into core soon! best regards Andreas