|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-11-15 06:04 UTC] daniel dot oconnor at gmail dot com
Description:
------------
DomDocument::createElement() should warn you if you create invalid XML.
Reproduce code:
---------------
<?php
$string = '<tree><branch>Fun Games &</branch></tree>';
$xml = new SimpleXMLElement($string);
$xml->addChild('actor', 'John & Doe');
print $xml->asXML();
$dom = new domDocument;
$dom->loadXML($string);
$dom->appendChild($dom->createTextNode("fish & & chips"));
$node = $dom->createElement('fish', 'ampersand & this, &');
$dom->appendChild($node);
print $dom->saveXML();
Expected result:
----------------
A warning when you do the createElement about the unfinished entity; or at least when you try the saveXML
Actual result:
--------------
---------- php ----------
Warning: SimpleXMLElement::addChild(): unterminated entity reference Doe in C:\vx\tests\simplexml.php on line 6
<?xml version="1.0"?>
<tree><branch>Fun Games &</branch><actor>John </actor></tree>
<?xml version="1.0"?>
<tree><branch>Fun Games &</branch></tree>
fish &amp; & chips
<fish>ampersand & this, &</fish>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 21:00:02 2025 UTC |
Description: ------------ The second argument ($value) in DOMDocument::createElement()/DOMDocument::createElementNS() is not escaped properly. To be more exact "&" is not escaped, "<" and ">" are. This result in a warning, and not all content is added to the text node inside the created element node. Reproduce code: --------------- $dom = new DOMDocument; $dom ->appendChild($dom->createElement('element', 'B & B')); echo $dom->saveXml(); Expected result: ---------------- <?xml version="1.0"?> <element>B & B</element> Actual result: -------------- Warning: DOMDocument::createElement(): unterminated entity reference B in /tmp/execpad-c7cffb3796e4/source-c7cffb3796e4 on line 4 <?xml version="1.0"?> <element>B </element> Additional Information ---------------------- The bug can be avoided if the text node is created separately and appended to the element node. $dom = new DOMDocument; $dom ->appendChild($dom->createElement('element')) ->appendChild($dom->createTextNode('B & B')); echo $dom->saveXml(), "\n";