#46047 [Opn->Csd]: SimpleXML converts empty nodes into object with nested array

From: Date: Thu, 11 Sep 2008 14:25:59 +0000
Subject: #46047 [Opn->Csd]: SimpleXML converts empty nodes into object with nested array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-128884@lists.php.net to get a copy of this message
ID: 46047 Updated by: rrichards@php.net Reported By: dalef at yahoo-inc dot com -Status: Open +Status: Closed Bug Type: SimpleXML related Operating System: FreeBSD 4 PHP Version: 5.2CVS-2008-09-10 (snap) New Comment: This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. Previous Comments: ------------------------------------------------------------------------ [2008-09-10 21:37:09] dalef at yahoo-inc dot com Description: ------------ When I run the following script I get strange results that are difficult to test against, and when converted to json, a real pain to use on the client-side. <?php $xml = new SimpleXMLElement('<foo><bar><![CDATA[]]></bar><baz/></foo>', LIBXML_NOCDATA); print_r($xml); $json = json_encode($xml); echo "{$json}\n"; ?> Since bar is empty (Even though it has a CDATA tag that is removed) before it gets to me ... I should get something like this bar => "" OR at most ... bar => SimpleXMLElemebt object () but ... what I don't expect is this: SimpleXMLElement Object ( [bar] => SimpleXMLElement Object ( [0] => ) ... When this data is converted to json ... I get bar:{"0":""} which requires me to do typeof and other types of tests on the client-side. I mentioned this issue to our resident FE PHP expert here at Y! and they said to file a bug. Reproduce code: --------------- Run this script on the command line: <?php $xml = new SimpleXMLElement('<foo><bar><![CDATA[]]></bar><baz/></foo>', LIBXML_NOCDATA); print_r($xml); $json = json_encode($xml); echo "{$json}\n"; ?> Expected result: ---------------- SimpleXMLElement Object ( [bar] => "" ) OR SimpleXMLElement Object ( [bar] => SimpleXMLElement Object ( ) ) Actual result: -------------- SimpleXMLElement Object ( [bar] => SimpleXMLElement Object ( [0] => "" ) ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46047&edit=1

« previous php.bugs (#128884) next »