note 73617 added to function.dom-domdocument-createelement
| From: | mikek dot nospam at nospam dot muonics dot com | Date: | Sun, 04 Mar 2007 01:24:02 +0000 |
| Subject: | note 73617 added to function.dom-domdocument-createelement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124062@lists.php.net to get a copy of this message | ||
With regard to the note below about needing htmlentities to avoid warnings about unterminated entity
references, I thought it worthwhile to mention that that you don't need to with createTextNode
and DOMText::__construct. If you mix both methods of setting text nodes and do (or don't)
apply htmlentities consistently to all data to be displayed, you'll get &s (or warnings
and badly-formed xml).
It's probably in one's best interest to extend DOMElement and DOMDocument so that it
creates a DOMText node and appends it, rather than passing it up to the DOMElement constructor.
Otherwise, good luck using (or not using) htmlentities in all the right places in your code,
especially as code changes get made.
<?php
class XDOMElement extends DOMElement {
function __construct($name, $value = null, $namespaceURI = null) {
parent::__construct($name, null, $namespaceURI);
}
}
class XDOMDocument extends DOMDocument {
function __construct($version = null, $encoding = null) {
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', 'XDOMElement');
}
function createElement($name, $value = null, $namespaceURI = null) {
$element = new XDOMElement($name, $value, $namespaceURI);
$element = $this->importNode($element);
if (!empty($value)) {
$element->appendChild(new DOMText($value));
}
return $element;
}
}
$doc1 = new XDOMDocument();
$doc1_e1 = $doc1->createElement('foo', 'bar & baz');
$doc1->appendChild($doc1_e1);
echo $doc1->saveXML();
$doc2 = new XDOMDocument();
$doc2_e1 = $doc2->createElement('foo');
$doc2->appendChild($doc2_e1);
$doc2_e1->appendChild($doc2->createTextNode('bar & baz'));
echo $doc2->saveXML();
?>
Text specified in createElement:
<?xml version=""?>
<foo>bar & baz</foo>
Text added via createTextNode:
<?xml version=""?>
<foo>bar & baz</foo>
----
Server IP: 64.71.164.2
Probable Submitter: 66.92.7.20
----
Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-createelement.php
Edit -- https://master.php.net/note/edit/73617
Del: integrated -- https://master.php.net/note/delete/73617/integrated
Del: useless -- https://master.php.net/note/delete/73617/useless
Del: bad code -- https://master.php.net/note/delete/73617/bad+code
Del: spam -- https://master.php.net/note/delete/73617/spam
Del: non-english -- https://master.php.net/note/delete/73617/non-english
Del: in docs -- https://master.php.net/note/delete/73617/in+docs
Del: other reasons-- https://master.php.net/note/delete/73617
Reject -- https://master.php.net/note/reject/73617
Search -- https://master.php.net/manage/user-notes.php