|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-08-01 14:37 UTC] php at sebastianmendel dot de
Description:
------------
When laoding XML into SimpleXMLElement and iterating with ->children() empty() reports true for SimpleXMLElement objects.
In PHP 5.3.16 empty() returns false.
In PHP 5.4.5 empty() returns true.
Test script:
---------------
$xml = <<<EOT
<?xml version="1.0" ?>
<Y><X>6098881</X></Y>
EOT;
$sxml = new SimpleXMLElement($xml);
foreach ($sxml->children() as $xmlKey => $xmlNode) {
var_dump(
empty($xmlNode), (string) $xmlNode, $xmlNode->asxml()
);
}
$xmlNode = new SimpleXMLElement('<X>501936634a721</X>');
var_dump(
empty($xmlNode), (string) $xmlNode, $xmlNode->asxml()
);
Expected result:
----------------
bool(false)
string(7) "6098881"
string(14) "<X>6098881</X>"
bool(false)
string(13) "501936634a721"
string(43) "<?xml version="1.0"?>
<X>501936634a721</X>
"
Actual result:
--------------
bool(true)
string(7) "6098881"
string(14) "<X>6098881</X>"
bool(false)
string(13) "501936634a721"
string(43) "<?xml version="1.0"?>\n<X>501936634a721</X>\n"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 17:00:01 2025 UTC |
I don't know if is exactly the same bug, but I have a similar problem. The empty function is returning true when accessing the sub-elements using the main object, and is returning false when accessing the sub-elements using the variable containing the sub-element. Example script: $xmlstr = '<?xml version="1.0" standalone="yes"?> <movies> <movie> <title></title> </movie> </movies>'; $movies = new SimpleXMLElement($xmlstr); $subelement = $movies->movie->title; echo "Accesing normal: " . PHP_EOL; echo var_dump($movies->movie->title) . PHP_EOL; echo var_dump(empty($movies->movie->title)) . PHP_EOL; echo "Accesing subelement: " . PHP_EOL; echo var_dump($subelement) . PHP_EOL; echo var_dump(empty($subelement)) . PHP_EOL;I guess my problem is related to this bug: TEST: ----------------------- $xmlString = '<?xml version="1.0" ?><root><node>not empty</node><node></node><node attr="attr"></node></root>'; $xmlObject = new SimpleXMLElement($xmlString); echo $xmlString; echo $xmlObject->asXML(); Expected result: ------------------- <root> <node>not empty</node> <node></node> <node attr="attr"></node> </root> Actual result: ------------------- <root> <node>not empty</node> <node> <node attr="attr"></node> </node> </root>