php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46047 SimpleXML converts empty nodes into object with nested array
Submitted: 2008-09-10 21:37 UTC Modified: 2008-09-11 14:25 UTC
From: dalef at yahoo-inc dot com Assigned:
Status: Closed Package: SimpleXML related
PHP Version: 5.2CVS-2008-09-10 (snap) OS: FreeBSD 4
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dalef at yahoo-inc dot com
New email:
PHP Version: OS:

 

 [2008-09-10 21:37 UTC] 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] => ""
    )
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-11 14:25 UTC] rrichards@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC