note 93425 modified in simplexmlelement.addChild by danbrown

From: Date: Tue, 03 Nov 2009 15:53:25 +0000
Subject: note 93425 modified in simplexmlelement.addChild by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162363@lists.php.net to get a copy of this message
Warning: addChild doesn't escape like addAttribute does. For example: <?php $test = '<test/>'; $msg = "This is a test & stuff."; $xml = new SimpleXMLElement($test); $xml->addAttribute('foo', $msg); echo htmlentities($xml->asXML()); $xml->addChild('bar', $msg); echo htmlentities($xml->asXML()); ?> Outputs: <?xml version="1.0"?> <test foo="This is a test &amp; stuff."/> Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference stuff. in file.php on line 13 <?xml version="1.0"?> <test foo="This is a test &amp; stuff."><bar>This is a test </bar></test> Instead of the expected: <?xml version="1.0"?> <test foo="This is a test &amp; stuff."><bar>This is a test &amp; stuff.</bar></test> This can be avoided by using the following: <?php // Generates an error: // $xml->addChild('bar', $msg); // Works as expected: $bar = $xml->addChild('bar'); $bar[0] = $msg; ?> --was-- Warning: addChild doesn't escape like addAttribute does. For example: <?php $test = '<test/>'; $msg = "This is a test & stuff."; $xml = new SimpleXMLElement($test); $xml->addAttribute('foo', $msg); echo htmlentities($xml->asXML()); $xml->addChild('bar', $msg); echo htmlentities($xml->asXML()); ?> Outputs: <?xml version="1.0"?> <test foo="This is a test &amp; stuff."/> Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference stuff. in file.php on line 13 <?xml version="1.0"?> <test foo="This is a test &amp; stuff."><bar>This is a test </bar></test> Instead of the expected: <?xml version="1.0"?> <test foo="This is a test &amp; stuff."><bar>This is a test &amp; stuff.</bar></test> http://php.net/manual/en/simplexmlelement.addChild.php

« previous php.notes (#162363) next »