|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-03-04 13:14 UTC] fred5 at originsystems dot co dot za
Description:
------------
I have tried to json_encode a DOMDocument and does not return false and does not throw exception.
Instead just returns {}, entirely ignoring the contents of the DOMDocument it is meant to be decoding.
The problem for me here is that the json_encode logically "fails" but does not indicate this.
(In an ideal world I would expect it to decode the DOMDocument in exactly the same way as it does a SimpleXML document
Test script:
---------------
<?php
$a = new DOMDocument();
$a->appendChild($a->createElement("hello","there"));
print "Encode DOMDocument" . PHP_EOL;
if (($output = json_encode($a)) === false) {
print "fails with error!";
} else {
print $output;
}
print PHP_EOL . PHP_EOL;
print "Encode DOMDocument document element" . PHP_EOL;
if (($output = json_encode($a->documentElement)) === false) {
print "fails with error!";
} else {
print $output;
}
print PHP_EOL . PHP_EOL;
print "Encode SimpleXML equivalent" . PHP_EOL;
if (($output = json_encode(simplexml_import_dom($a->documentElement))) === false) {
print "fails with error!";
} else {
print $output;
}
print PHP_EOL . PHP_EOL;
Expected result:
----------------
I would expect either DOMDocument or DOMDocument document element to encode sensibly.
Or at the very least I would expect json_encode to return false or throw exception should the failure be considered catastrophic.
Actual result:
--------------
Encode DOMDocument
{}
Encode DOMDocument document element
{}
Encode SimpleXML equivalent
{"0":"there"}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 19:00:01 2025 UTC |
Apologies, the description should read ... Instead just returns {}, entirely ignoring the contents of the DOMDocument it is meant to be ENcoding. ...