note 95545 added to class.domnode
| From: | DavidRekowski at osu1 dot php dot net | Date: | Fri, 08 Jan 2010 09:54:10 +0000 |
| Subject: | note 95545 added to class.domnode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164776@lists.php.net to get a copy of this message | ||
You cannot simply overwrite $textContent, to replace the text content of a DOMNode, as the missing
readonly flag suggests. Instead you have to do something like this:
<?php
$node->removeChild($node->firstChild);
$node->appendChild(new DOMText('new text content'));
?>
This example shows what happens:
<?php
$doc = DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
echo "Content 1: ".$node->textContent."\n";
$node->textContent = 'new content';
echo "Content 2: ".$node->textContent."\n";
$newText = new DOMText('new content');
$node->appendChild($newText);
echo "Content 3: ".$node->textContent."\n";
$node->removeChild($node->firstChild);
$node->appendChild($newText);
echo "Content 4: ".$node->textContent."\n";
?>
The output is:
Content 1: old content // starting content
Content 2: old content // trying to replace overwriting $node->textContent
Content 3: old contentnew content // simply appending the new text node
Content 4: new content // removing firstchild before appending the new text node
If you want to have a CDATA section, use this:
<?php
$doc = DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
$node->removeChild($node->firstChild);
$newText = $doc->createCDATASection('new cdata content');
$node->appendChild($newText);
echo "Content withCDATA: ".$doc->saveXML($node)."\n";
?>
----
Server IP: 212.124.37.9
Probable Submitter: 89.246.128.28
----
Manual Page -- http://www.php.net/manual/en/class.domnode.php
Edit -- https://master.php.net/note/edit/95545
Del: integrated -- https://master.php.net/note/delete/95545/integrated
Del: useless -- https://master.php.net/note/delete/95545/useless
Del: bad code -- https://master.php.net/note/delete/95545/bad+code
Del: spam -- https://master.php.net/note/delete/95545/spam
Del: non-english -- https://master.php.net/note/delete/95545/non-english
Del: in docs -- https://master.php.net/note/delete/95545/in+docs
Del: other reasons-- https://master.php.net/note/delete/95545
Reject -- https://master.php.net/note/reject/95545
Search -- https://master.php.net/manage/user-notes.php